C++ TEXT FILE | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ TEXT FILE

Hi guys , I have a little problem. I am making a program that asks user like his name and surname and then create a txt file with this information: EXAMPLE: cout << "Enter name: "; getline(cin , name); cout << "Enter surname: " ; getline(cin , surname); //OUTPUT IN THE TXT FILE Name: Name; Surname: Surname; The problem is if the user wants to change like the name , how can I access to the line where can I change only the surname the user entered. Thanks. Happy New Year.

1st Jan 2021, 5:59 PM
Hammad Yaqub
Hammad Yaqub - avatar
5 Answers
+ 2
You can read the values in like this and confirm that the user typed the intended value: int confirmation; do { cout << "Enter name: "; getline(cin , name); cout << "You entered " << name << ". If this is correct, enter 1. Otherwise enter 0." << endl; cin >> confirmation; } while (confirmation != 1); do { cout << "Enter surname: " ; getline(cin , surname); cout << "You entered " << surname << ". If this is correct, enter 1. Otherwise enter 0." << endl; cin >> confirmation; } while (confirmation != 1); Do you want to edit the file after it was already made?
1st Jan 2021, 6:26 PM
Josh Greig
Josh Greig - avatar
0
@josh Greig I am making a banking software , that there are many options like the 1st is to create account, and the user creates an account , and his info like name & surname are going to be saved in txt file. Then after the program has been created , he has a second option in the menu to change his info.
1st Jan 2021, 6:59 PM
Hammad Yaqub
Hammad Yaqub - avatar
0
@Mirielle Thanks , I didnt know about that , but I'll search about that ,thanks.
1st Jan 2021, 7:01 PM
Hammad Yaqub
Hammad Yaqub - avatar
0
Check out this loop containing a switch statement for how you could implement your user-interface: https://www.studytonight.com/c/programs/misc/menu-driven-program You could have a similar loop containing a switch statement in your program. You probably want the following cases: - Save to file. - Replace name with user input and print the new value - Replace surname with user input and print the new value - Print name and surname Like one of the other answers suggested, your text file format is a little odd, though. If I was doing it, I'd remove the "Name:" and "Surname:" and just have contents like this: John Smith JSON is a nice format in general but c and c++ require libraries to parse JSON and serialize JSON properly so the very simple 2 line text format above may be better. If the file exists when starting your program, you should try reading in the name and surname from the file before showing that menu. If your software ends up managing much more sensitive information, you really need more senior technical people involved in the project and reviewing your work. Banking information is incredibly sensitive and targeted by malicious hackers. It sounds like you're a beginner programmer that shouldn't be making software that deals with any sensitive data because a beginner can easily make terrible mistakes like unhashed and unencrypted passwords, credit card numbers... going into files or emails.
2nd Jan 2021, 4:02 AM
Josh Greig
Josh Greig - avatar
0
@Josh Greig Thanks for your advices. By the way thi software is only a challege for me , because I want to know what can I do. I just finish creting the first option which is to create , where console asks the user info and then save it in a txt file in folder. Now I want to know how to get access to this data and change it, beacuse if user wants to update data , he can do it. I also made a login method which asks the user name and surname and also the password creted by the user while registration , and then if (passwordUserEntered == passwordAccountCreatedWhileRegistration) --> gwt access to file and permit him to change So I want to get access to line of the password and read it and then confront it with the password user entered. By the way thanks a lot , for your time and the info you gave me
2nd Jan 2021, 1:53 PM
Hammad Yaqub
Hammad Yaqub - avatar