Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 re - initializing a variable
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jonos




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Thu Feb 12, 2004 11:11 pm   Post subject: (No subject)

code:

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


all should work
maybe some more the code would help
jonos




PostPosted: Fri Feb 13, 2004 11:57 am   Post subject: (No 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.
wtd




PostPosted: Fri Feb 13, 2004 1:19 pm   Post subject: (No subject)

You want to have declared

code:
int point = 0;


at the beginning of this function.
Andy




PostPosted: Sat Feb 14, 2004 5:39 pm   Post subject: (No subject)

int points=0; u mean
wtd




PostPosted: Sat Feb 14, 2004 6:21 pm   Post subject: (No subject)

dodge_tomahawk wrote:
int points=0; u mean


Typo. So sue me! Razz
McKenzie




PostPosted: Sat Feb 14, 2004 6:25 pm   Post subject: (No 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
jonos




PostPosted: Sat Feb 14, 2004 11:41 pm   Post subject: (No 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
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: