Simple C++ program having funky output

Why does the code I made have a funky number output? It just converts lower case to upper case and vice versa. It also echos the words you typed in, and leaves out numbers you typed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    int space;
    int letter;
    int notLetters;
    char ch;
 cout << "I will read all keyboard input until you type \"@\" OK" << endl;
 cin.get(ch);
 while(cin.get(ch) && ch != '@'){
          if (isalpha(ch)){
             letter++;
             if (isupper(ch)){
                cout << tolower(ch);
                }
                else if (!(isupper(ch))){
                     cout << toupper(ch);
                     }
             }
          else if (ispunct(ch)){
               space++;
               }
          else if (!(isalpha(ch))){
               notLetters++;
               }
               cin.get(ch);
          }
          cin.get();
          cout << "You typed in " << letter << " letters, and " << notLetters <<
           " things that arent letters. You also typed in " << space << " words.";
  cin.get();
  cin.get();
}
You didn't set letters or notLetters or space to zero, so they begin at some random value.
Last edited on
My first guess is that you have far too many cin.get() calls in your program. You should probably only have one (the one in the while()).

Next be aware of the type of variables the <cctype> functions return (hint: it's not a character). Since you just print those return values they will not be converted to a char.

You should also initialize all your variables before you use them.



Okay thanks. That was my first time using the <cctype> functions. I'll make sure to type cast their return values to char to fix it. Also the cin.get() stop the program from closing immediately in console.
Okay so now, with the fixed code, for some reason it doesn't completely echo the word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    int space = 0;
    int letter = 0;
    int notLetters = 0;
    char ch;
 cout << "I will read all keyboard input until you type \"@\" OK" << endl;
 cin.get(ch);
 while(cin.get(ch) && ch != '@'){
          if ((char)isalpha(ch)){
             letter++;
             if ((char)isupper(ch)){
                cout << (char)tolower(ch);
                }
                else if (!((char)isupper(ch))){
                     cout << (char)toupper(ch);
                     }
             }
          else if ((char)ispunct(ch)){
               space++;
               }
          else if (!((char)isalpha(ch))){
               notLetters++;
               }
               cin.get(ch);
          }
          cin.get();
          cout << "You typed in " << letter << " letters, and " << notLetters <<
           " things that arent letters. You also typed in " << space << " words.";
  cin.get();
  cin.get();
}

like if I enter "Hello@"
it returns "el"
Last edited on
zarman wrote:
Okay so now, with the fixed code, for some reason it doesn't completely echo the word.
jlb wrote:
My first guess is that you have far too many cin.get() calls in your program.


... might be related.
That was my first time using the <cctype> functions. I'll make sure to type cast their return values to char to fix it.

You wouldn't need to cast the return values if you assign the return values to a char.

char ch = toupper(ch);
Your problem is being caused because you're not storing the return value in a char variable. Remember the stream operator will see that function is returning an int and will place the returned value in a temporary int variable for the print.

Also the cin.get() stop the program from closing immediately in console.

This is true only for the very last cin.get(), not the many other cin.get() strewn about the program.
Topic archived. No new replies allowed.