-3

Wanting to do some fancy formatting. I have several lines that I want to interact with each other. Get the first two lines. Print out the character in the second line times the integer in the first line. Seperate them all with a asterisk character. No asterisk after the final character is printed. Move onto the next integer and character. Print them on a separate line. Do this for the whole list. The problem I am having is printing them on separate lines. Example:

5
!
2
?
3
#

Desired output:

!*!*!*!*!
?*?
#*#*#

My output:

!*!*!*!*!*?*?*#*#*#*

Below is my code. Another thing to mention is that I am reading the data about the characters and numbers from a separate text file. So I am using the getline function.

Here is a chunk of the code:

ifstream File
File.open("NumbersAndCharacters.txt")
string Number;
string Character;
while(!File.eof(){
  getline(File, Number);
  getline(File, Character);
//a few lines of stringstream action
  for (int i=0; i<=Number; i++){
      cout<<Character<<"*";}//end for. I think this is where
                            //the problem is.
  }//end while
File.close();
return 0;

Where is the error? Is it the loop? Or do I not understand getline?

It should be printing an "endl" or "\n" after each multiplication of the character is done.

Thanks to everyone for the responses!

Matt
  • 74,352
  • 26
  • 153
  • 180
brrnrr_47
  • 13
  • 5

1 Answers1

0

You have not shown your code yet, but what seems to be the issue here is that you simply forgot to add a new line every time you print your characters. For example, you probably have done:

std::cout << "!";

Well, in this context you forgot to add the new line ('\n'), so you have two options here: first insert the new line yourself:

std::cout << "! \n";

Or std::endl;

std::cout << "!" << std::endl;

For comparison of the two, see here and here. Without further description, or more importantly your code that doesn't seem to work properly, we can't make suggestions or solve your problem.

Community
  • 1
  • 1
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • That doesn't address the second issue, the superfluous trailing `*` character. – IInspectable Oct 02 '16 at 22:37
  • @IInspectable well , he didn't show code so this is a complete prediction. I am guessing he printed using a for loop and he accidently had one more iteration than needed? But if he doesn't show code , how am i suppose to know what caused the " superfluous trailing `*` character" ? – amanuel2 Oct 02 '16 at 22:39
  • 1
    You aren't supposed to know. Neither are you supposed to answer a question, that's off-topic. – IInspectable Oct 02 '16 at 22:53