Computer Science Canada

re - initializing a variable

Author:  jonos [ Thu Feb 12, 2004 10:40 pm ]
Post subject:  re - initializing a variable

i don't think that is the word for this but i want to set a variable to add one to itself after an if statment. i have tried:

points = points+1
points++
points+1
points = points++

and they don't work.

this is what im trying to do:
code:

cout << "is the donkey going north?" << endl;
cin >> ans;
if (ans =="y")
{
points++;
}


if anyone knows the problem, could you ;lease help me thanks

Author:  Catalyst [ Thu Feb 12, 2004 11:11 pm ]
Post subject: 

code:

points++;
points = points+1 ;
points+=1;


all should work
maybe some more the code would help

Author:  jonos [ Fri Feb 13, 2004 11:57 am ]
Post subject: 

lame communist exposer:

code:

cout << "Have you ever felt pride in hearing the exclamation of Mother Russia?" << endl;
cin >> ans;
if (ans == "y")
{
points ++;
}
cout << points << endl;

cout << "Have you ever read the Communist Manifesto, and thought it very enlightening?" << endl;
cin >> ans;
if (ans == "y")
{
points++;
}

cout << "Have you ever visited sovietempire.com?" << endl;
cin >> ans;
if (ans =="y")
{
points+1;
}

cout << "Communist Answers Communicated: ";
cout << points;


it compiles and everything, but it when it says
Communist Answers Communicated, it says 0 instead of what should be there, such as 1, 2, or 3 depending on the answer given.

Author:  wtd [ Fri Feb 13, 2004 1:19 pm ]
Post subject: 

You want to have declared

code:
int point = 0;


at the beginning of this function.

Author:  Andy [ Sat Feb 14, 2004 5:39 pm ]
Post subject: 

int points=0; u mean

Author:  wtd [ Sat Feb 14, 2004 6:21 pm ]
Post subject: 

dodge_tomahawk wrote:
int points=0; u mean


Typo. So sue me! Razz

Author:  McKenzie [ Sat Feb 14, 2004 6:25 pm ]
Post subject: 

My guess is that the problem is probably with
Quote:
ans == "y"

how was ans declared:
1. char ans;
use ans =='y'
2. char ans[2];
use strcmp(ans,"y")==0
3. string ans;
ans == "y" // i.e. my guess was wrong

Author:  jonos [ Sat Feb 14, 2004 11:41 pm ]
Post subject: 

wow, thanks mckenzie, that was the problem, i had the variable as:

char points[1];
for some reason, i forgot to take out the one cause that is useless i think for my purpose cause char is already 1.

it works now


: