C++ guidance

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

vladteppi

Thread Starter
Joined
Oct 5, 2011
Messages
1
Hello, I am making a function that will input a file in C++. Then, it will ask the user what the width of the lines will be. Once it does that, it will output the words from the text file until it reaches that character width limit and then it will go to the next line. It will continue until all the words in the file are out. I don't know what to do. I was thinking about running a char array but I don't know how I would set that up. Could you give me some ideas?
 
Joined
Sep 9, 2006
Messages
60
If this is a small plain text file, you could *probably* append a string as you loop through your file and then output the required pattern.

Code:
std::string s = "", file_line_read;
int pos = 0, length_of_line = some_user_specified_value;
... // bits of code
file_read_loop(while_file_is_valid) {
   s += file_line_read;
}
... bits of code
// output
while(pos < s.length()) {
   std::cout << s.substr(pos, length_of_line) << std::endl;
   pos += length_of_line;
}
... bits of code
If you post what you've tried, someone skilled in cpp will likely be able to give you a more direct answer.
HTH
 
Joined
Apr 30, 2001
Messages
2,636
Just in case you're still working on this:

Then, it will ask the user what the width of the lines will be. Once it does that, it will output the words from the text file until it reaches that character width limit and then it will go to the next line.
What happens if the limit falls in the middle of a word? Are you supposed to discard the partial text of the word?

When you display all the words for the line, can you display them all on one line separated by a space? If so and the line (the limited length of it) ends with a space, do you have to remove that space?

Or, are you supposed to display each word for a line on a separate line?

How big is the file you're reading?

If this is homework, what things in C++ are you allowed to use? And, what are you currently working on in the class? As in, what's this assignment specifically supposed to teach you?

All those clues can get you a better response.
 
Joined
Oct 17, 2011
Messages
3
I need some suggestions/guidance regarding C++.Well, first of all, I'd like to write a couple of lines about my background and my current situation: I studied math in Spain ( 5 years of uni in Spain, more years than other countries ) and right after that I worked for an english bank branch on my country ( Barclays Bank), in risk department, for a year and a half. Then I decided I wanted to study a little bit more in order to change into a more quantitative area (as a branch, we didn't do big things, everything was done in London ) so in october I will start my Quantitative Finance Master program, which is sponsored by Banco Santander, biggest entity here around.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Top