•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,575 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1427 | Replies: 5
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
Ok i have a file of names that looks like this:
im trying to use a for loop to store each name in a variable or string so i can use a for loop again later to output each name
im pretty new to c++ and i cant figure this out
i can only figure out how to read 1 line from a file
thanks in advanced to anyone that helps
john mary joe matthew
im trying to use a for loop to store each name in a variable or string so i can use a for loop again later to output each name
im pretty new to c++ and i cant figure this out
i can only figure out how to read 1 line from a file
thanks in advanced to anyone that helps
Try using a
Since you seem to know code tags, you should be aware that for really good help you need to post your code...
while look for the first loop instead. A for loop implies you know how many times you need to go through the loop. A while implies you exit the loop when some criteria is satisfied, such as you reached the end-of-file (EOF).Since you seem to know code tags, you should be aware that for really good help you need to post your code...
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
Well. Regardless of the loop kind (assuming it's allowed to iterate enough times), it should read in the info:
Where in is your input file stream and word is something used to store the information of the file.
while ( in >> word ) std::cout<< word;
Where in is your input file stream and word is something used to store the information of the file.
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
well here is the code i have
Im trying to put the name on each line into a separate array so after i could do something along the lines of
int main()
{
char name[20];
ifstream infile;
infile.open("names.txt", ios::in);
if (infile)
{
infile.get(name, 20);
}
else
cout << "Error Opening File" << endl;
infile.close();
return 0;
}Im trying to put the name on each line into a separate array so after i could do something along the lines of
while (i != 0){
cout << "Name: " << name[i] << endl;
i++;
} Last edited by blindkilla : Oct 27th, 2007 at 5:45 pm.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
you will need an array of strings. There's a couple ways to do it
// this array will hold up to 100 names and each name can be up to 19 characters long. char names[100][20]; // This is a c++ array that can contain an unlimited (almost) number of names // and each name can be unlimited (almost) length vector<string> names;
Last edited by Ancient Dragon : Oct 27th, 2007 at 6:53 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How can I delete empty lines from file? (Java)
- Reading multiple lines from a txt file (C++)
- Find the total lines in the file (Shell Scripting)
- reading numbers from a text file and putting them into an integer (C++)
- print text in multiple lines in picturebox in vb6 (Visual Basic 4 / 5 / 6)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
Other Threads in the C++ Forum
- Previous Thread: Best language for writing OMR program?
- Next Thread: Simple ATM, just need to add one more thing



Linear Mode