• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ n00b question

bluewall21

Golden Member
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;}
 
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;
}
 
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??
 
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!
 
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!
 
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.
 
Back
Top