There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop mac malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows vista windows xp winxp wireless wmp
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Another C++ Q


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
hyler's Avatar
Senior Member with 120 posts.
 
Join Date: Jul 2001
01-Oct-2003, 11:37 AM #1
Another C++ Q
When I compile my program, the IF statements fail to work correctly. I ask the user to input my newly defined int variable, and have the IF statements run off of whatever they put in, but no matter what they enter, it starts up on the first IF statement.

int choice;
cin>>choice;
if (choice = 1)
cout<<"Addition-";
cout<<"Enter first number: ";
int first;
cin>>first;
cout<<"Enter second number: ";
int second;
cin>>second;
cout<<first + second;
goto start;
if (choice = 2)
cout<<"Subtraction-";
cout<<"Enter first number: ";
int first1;
cin>>first1;
cout<<"Enter second number: ";
int second1;
cin>>second1;
cout<<first1 - second1;
goto start;
if (choice = 3)
cout<<"Multiplication-";
cout<<"Enter first number: ";
int first2;
cin>>first2;
cout<<"Enter second number: ";
int second2;
cin>>second2;
cout<<first2 * second2;
goto start;
if (choice = 4)
cout<<"Division-";
cout<<"Enter first number: ";
int first3;
cin>>first3;
cout<<"Enter second number: ";
int second3;
cin>>second3;
cout<<first3 / second3;
goto start;
}
deuce868's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
01-Oct-2003, 12:02 PM #2
to check you need two = signs, not just one. All you are doing is setting choice = 1 each time.
Guy's Avatar
Guy Guy is offline
Senior Member with 260 posts.
 
Join Date: Feb 1999
Location: abby, BC, canada
03-Oct-2003, 05:11 PM #3
Just thought I would add that a good coding habit to get into that will help new coders (and advanced one too :P) locate this common error easily is to put the constant as the first element in the check. If you do this the error will be produced at compile time and is much easier to remedy.

ie:
Code:
// do it like this
if(1 == choice)
{
...
}
// rather than like this
if(choice == 1)
{
...
}
if you leave out an = sign in the first if statement, you will get a compile time error because you cannot assing a value to a constant, but if you do it the second way, the program will run, but it will behave strangely and unexpectedly.

Cheers
__________________
There are only 10 types of people in the world; Those who understand binary, and those who dont.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:47 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.