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();

Recommended Answers

All 2 Replies

So whats wrong with it? Does it not work or are you getting a compiler error?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.