Why things always go wrong?

Hi!
Actually, i'm too beginner to ask such a question, I just started C++ and I'm reading the whole Documentation part of this site!
But when I write programs, sometimes things doesn't work correctly!
For example the last program I was trying to write was:

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	
char a,b,c; int d,e=0;string suffix;
	cout<< "How many numbers you got for me to sum up?"<<endl;
	cin>>a;
	
for (b=1;b<=a;b++)
	{
		c=b%10;
switch (c)
		{
case 1:
			suffix=("st");
break;
case 2:
			suffix=("nd");
break;
case 3:
			suffix=("rd");
default:
			suffix=("th");
		}
		cout<<"Enter the "<<b<<suffix<<" number :"<<endl;
		cin>>d;
		e=d+e;
	}
	cout<<e;
}


As you see I wanted it to write down something like this: 1st, 2nd, 3rd, 4th, 5th, ... (I know it's not correct for 11, 12 and 13, but that doesn't matter) but it doesn't work! Instead of writing numbers, it draws some funny pics! (In VisualStudio>Win32ConsoleApplication>C++Class)
neither the whole program, nor the part bthat is supposed to write down 12t, 2nd & ...!
What is wrong?
Is it because i'm working with Visual Studio? (Win32ConsoleApplication)
Or is it something wrong with the code?
or ... ?
Last edited on
Ok, you are confused about types of your variables.

Variables a and b and c should be ints, not char.

Also don't put multiple statements on one line, do this instead:

1
2
3
4
5
6
7
int a;
int b;
int c; 

int d
int e=0;
string suffix;


If you just wanted to deal with the number 1 to 5, I think a for loop would be easier than the switch. The logic is much harder to get the right suffix for all the numbers.
Thank you! You're right, i'm confused with variables!
But as I see here: http://www.cplusplus.com/doc/tutorial/variables/
"char" is a small integer that I can use for number -127 to 128 (signed)
So for my numbers it should work!

I changed "char" to "int" and everything is working right!
correct me if i'm wrong:
"the four integer types char, short, int and long must each one be at least as large as the one preceding it"
(I copied it from the same page I gave you it's link above!)
So, if I want to use a 2 digits number, why char doesn't work?
The problem with char is the output and input operations. When you try to read a char like this cin>>a; it assumes that you want to read a character, so a will get the character value of the first digit.
Topic archived. No new replies allowed.