C++ n00b question

bluewall21

Golden Member
Feb 13, 2004
1,360
0
0
I made a little program for a friend, but the variable is only storing the first character of the name. Any fixes?

CODE:
#include <iostream.h>
int main ()
{char name;
cout<<"Submit Name:";
cin>>name;
cout<<"Processing"<<endl;
cout<<"Please wait"<<endl;
cout<<name<<", what a stupid name!!"<<endl;
return 0;}
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
A char is a character. So.. it only stores a character. That's some pre-standard C++, you probably want something like this:

#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout<<"Submit Name:";
cin>>name;
cout<<"Processing"<<endl;
cout<<"Please wait"<<endl;
cout<<name<<", what a stupid name!!"<<endl;
return 0;
}
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,007
126
Yes std::string is excellent; use it anytime you would use a char* or a char [].
 

bluewall21

Golden Member
Feb 13, 2004
1,360
0
0
Code
#include <iostream>
using namespace std;
int main ()
{float input=0;
float total=0;
char exit='n';
cout<<"Welcome to the square machine."<<endl;
cout<<"Enter number to be squared:";
cin>>input;
while (exit!='y');
{input*input=total;
cout<<"The square is:"total<<endl;
input=0;
total=0;
cout<<"Would you like to exit? (Y or N)";
cin>>exit;}
return 0;}

The first highlight is giving a "non-lvalue in assignment", and the second is giving a parse error.

Solutions??
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
input * input = total;

doesn't make any sense. What are you trying to do on that line?

edit: I think you want:

total = input * input;

but I'm not sure.

And somewhat OT: what's up with curly braces on lines of code! Yuck!
 

bluewall21

Golden Member
Feb 13, 2004
1,360
0
0
My friend porobably didn't know what he was talkinmg about, he probably told me the wrong thing for multiplication. HOpes that answers your question. And the curly braces are there because i changed the formatting to take up less space(room) on the forums. Don't hate, celebrate!
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: bluewall21
And the curly braces are there because i changed the formatting to take up less space(room) on the forums. Don't hate, celebrate!

Hah, ok. Well IMO, it makes it harder to read. It's nice to easily see where the braces are so you can easily tell when a block begins/ends.

And btw, this:

while (exit!='y');
{input*input=total;
cout<<"The square is:"total<<endl;
input=0;
total=0;
cout<<"Would you like to exit? (Y or N)";
cin>>exit;}

Looks like it's probably wrong. If the {} are supposed to be part of the while loop, then don't put a semicolon after the while.
 

bluewall21

Golden Member
Feb 13, 2004
1,360
0
0
ok, ill do some messing around until it works. Thanks for the help, you deserve the lifer