Page 1 of 1 [ 5 posts ] 

Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 28
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

21 Aug 2010, 7:00 pm

Hi every one, I am Blake.

I use to do a lot of programming on VB6 when I was younger but I'm only new to C++ and I can't figure out why this program to make a simple sentence does not work. ( works perfectly with numbers.)

Quote:

#include <iostream>

using namespace std;

main (void)


{

/* Values */
int a;
int b;
int c;
int d;
int e;

/* Some Sentences */
cout << " This is a program to write on your own sentences." << endl;
cout << " Write in your first word." << endl;

/* Input/Output */
cin >> a ;
cout << "" << endl;
cout << " Write in your second word." << endl;
cin >> b ;
cout << "" << endl;
cout << " Write in your third word." << endl;
cin >> c ;
cout << "" << endl;
cout << " Write in your fouth word." << endl;
cin >> d ;
cout << "" << endl;
cout << " Write in your fith word." << endl;
cin >> e ;
cout << "" << endl;
cout << "" << endl;

/* The Maths */
cout << a+b+c+d+e << endl;

/* System Commands */
system ("PAUSE");

}


Any help?

Blake;


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


kra17
Veteran
Veteran

User avatar

Joined: 14 Feb 2010
Age: 31
Gender: Male
Posts: 594
Location: Sweden

21 Aug 2010, 7:11 pm

You need to use string instead of int if you're going to store letters.
http://www.cplusplus.com/doc/tutorial/variables/


_________________
:bigsmurf: :bigsmurf:


Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 28
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

21 Aug 2010, 7:18 pm

Thank you;

Now I just need to fix the spaces problem:

Quote:
#include <iostream>

using namespace std;

main (void)


{

/* Values */
string a;
string b;
string c;
string d;
string e;

/* Some Sentences */
cout << " This is a program to write on your own sentences." << endl;
cout << " Write in your first word." << endl;

/* Input/Output */
cin >> a ;
cout << "" << endl;
cout << " Write in your second word." << endl;
cin >> b ;
cout << "" << endl;
cout << " Write in your third word." << endl;
cin >> c ;
cout << "" << endl;
cout << " Write in your fouth word." << endl;
cin >> d ;
cout << "" << endl;
cout << " Write in your fith word." << endl;
cin >> e ;
cout << "" << endl;
cout << "" << endl;

/* The Maths */
cout << a+b+c+d+e << endl;

/* System Commands */
system ("PAUSE");

}


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


Ichinin
Veteran
Veteran

User avatar

Joined: 3 Apr 2009
Gender: Male
Posts: 3,653
Location: A cold place with lots of blondes.

22 Aug 2010, 3:02 am

Blake_be_cool wrote:
Thank you;

Now I just need to fix the spaces problem:


Spaces? If you mean space between output between the variables, it can be fixed by changing:
Quote:
cout << a+b+c+d+e << endl;

to
Quote:
cout << a << " " << b << " " << c << " " << d << " " << e << endl;


And do not use + ever as a means to build output. Even in VB6 it can cause integers to be added up and you end up with a sum instead of an output. You can always process values as character variables by typecasting them implicitly with (char) in C/C++ or (string) in C#/Java or CStr() for VB.NET, or using the .toString() method after a variable in both Java and .Net languages.

Regardless of language, the best thing to do is to use a stringbuilder class when you build a textstructure that is going to be presented to the user.


_________________
"It is far better to grasp the Universe as it really is than to persist in delusion, however satisfying and reassuring" (Carl Sagan)


Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 28
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

22 Aug 2010, 3:08 am

Okey, Thank you.

I'm not sure if I understood all of that, but thank you.


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)