0

Im trying to learn working with text files and I been able to make a program adding text to a text file. However I have no clue how to make it so I'm able to add numbers to the text file. I want the program first ask for the name then for the age.
Like this:
How many names do you want to enter: 2
Enter name: Adam
Enter Age: 43
Enter name: Mark
Enter Age: 37

This is I have so far. Appreciating any help.

char vekt[50][30];
int antal;
cout<<"How many names do you want to enter:  ";
cin>>antal;
cin.get();   
    for(int i=0;i<antal;i++)
		{
			cout <<" Enter name: ";
			cin.getline(vekt[i],30);
		}

ofstream utskrift("data.txt",ios::app);
	if(!utskrift)
		{
			cout<<"could not find file"<<endl;
		}
for(int i=0;i<antal;i++)
	{
		utskrift<<vekt[i]<<endl;
	}
	utskrift.close();
3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Fbody
0

Um... How do you expect to add a number to the file if you never read a number? I don't see anything related to the "Enter Age: " prompt...

Once you add the proper input statements, the rest is basically the same as for the name.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.