-1

enter image description here I'm trying to get input from a file, but I need to do it one char at a time into a character array. There are no numbers or symbols, but it only prints out the first letter.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Handy reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 May 24 '22 at 23:22
  • Visual Studio has a world-class debugger. Use it to step through the program line by line and watch what happens as it happens. When the program does something you didn't expect, takes the wrong path or stores the wrong value usually, you've probably found a bug. [Here's some good documentation on using the debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2022) – user4581301 May 24 '22 at 23:31
  • 2
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/) – Remy Lebeau May 24 '22 at 23:49

1 Answers1

0

Your use of inFile.eof() is wrong. Move the read operation into the loop condition instead. And, consider using inFile.get(...) instead of inFile >> ... in this situation.

But, more importantly, your code prints out only 1 character because you are return'ing at the end of the 1st loop iteration. Get rid of the return statement inside the loop, it doesn't belong there at all.

Also, your cout statement inside the loop is printing out the wrong array element, because you are incrementing numofCharacters before passing the read character to cout. And, your loop has a potential buffer overflow, if the input file has more than 35 characters in it.

Try this loop instead:

while ((numofCharacters < 35) &&
       inFile.get(note1[numofCharacters]) /*inFile >> note1[numofCharacters]*/)
{
    cout << note1[numofCharacters];
    ++numofCharacters;

    /* or:
    ++numofCharacters;
    cout << note1[numofCharacters-1];
    */
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • okay thank you i tried it though it it didnt seem to work it still only shows that it printed the first letter still, eventually i have to put it into a sperate file and count the amount of lower and uppercase letters and combine the totals together and itll look something like this i just couted it to make sure it worked is all A = 36 B = 5 C = 3 . . . R = 4 S = 21 T = 10 . . . Z = 0 – Austin Rivera-Bennett May 25 '22 at 00:28
  • "*it didnt seem to work it still only shows that it printed the first letter*" - Then you are obviously still doing something wrong. Did you remove the offending `return` from the loop, like I mentioned? Please edit your question to show your updated code (as plain text, not as an image, please). – Remy Lebeau May 25 '22 at 00:35