PDA

View Full Version : Can anyone varify this for me...


Bmxpunk86pl
01-18-02, 06:09 PM
Ok i was wondering if you could use the "if" statement more than once like this:



//This is a Calculator made by Adam.
#include <iostream.h>
#include <stdio.h>


int main()
{
cout <<"****************************\n"; //Title of the program
cout <<"* CALCULATOR *\n";
cout <<"****************************\n";
cout <<"1=to divide\n";
cout <<"2=to multiply\n";
cout <<"3=to add\n";
cout <<"4=to subtract\n";
cout <<"\n";

int number;
cout <<"Please enter the number that\n";
cout <<"will be used in this math problem\n";
cin >>number;
if (number==1)
{
int number1;
cout <<"Please enter the first variable\n";
cin >> number1;

int number2;
cout <<"Please enter the second variable\n";
cin >> number2;

int answer;
answer = number1 /number2;
cout <<"The answer is " <<answer;
}
if (number==2)
(
int num1;
cout <<"Please enter the first variable\n";
cin >>num1;

int num2;
cout <<"Please enter the second variable\n";
cin >>num2;

int answer;
answer = num1 * num2;

cout <<"The answer is:"<<answer;
}



getchar(); //Program will not exit until it gets a character
}




the program is not finished and yea its kinda stupid but im new to c++.

_Will_
01-18-02, 06:34 PM
I could be wrong (I'm just beginnig programming in C++ myself) but I don't believe you can use if-else statements in that way.

1) If you want multiple Ifs you can either divide one IF statement into another If-Else statment. But not something I'd suggest. In VB there were things like Case 1, case 2 , etc but I'm not sure what that translates into in C++. I'm interested to know myself.

2) If statements can't stand alone, there must be a else to end it.

One thing I can just think of off the top of my head would be to do this:

if (boolean)
{statemants}
else
{do nothing} ( cout << "\n" or x = x or some other garbage)

probably not the best programming technique but it'd work


EDIT: I'll take a look in my book and see if I can't get some more info.

Bmxpunk86pl
01-18-02, 06:41 PM
ok thanks. Wait i could use the

switch ()

case1:

case 2:

thing.

_Will_
01-18-02, 06:44 PM
wait wait....stupid me

theres this:

if (boolean)
{statements}
else if (boolean)
{statements}
else if (boolean)
{statements}
else (boolean)
{statements}

(you can have unlimited else ifs)

XWRed1
01-18-02, 06:56 PM
Using a switch statement is probably a cleaner way to go about it, although either way will work.

Bmxpunk86pl
01-18-02, 06:56 PM
ok thanks a lot Will, and good luck with ur C++.

Bmxpunk86pl
01-18-02, 07:00 PM
wait, damn i forgot something. What do u mean by boolean?

_Will_
01-18-02, 07:41 PM
err...just pretend i said "inequality" ;)

but yeah...by boolean i just meant a true or untrue statement

Bmxpunk86pl
01-20-02, 02:18 PM
ok what the hell is wrong with my coding?


//This is a Calculator made by Adam.
#include <iostream.h>
#include <stdio.h>


int main()
{
cout <<"****************************\n"; //Title of the program
cout <<"* CALCULATOR *\n";
cout <<"****************************\n";
cout <<"1=to divide\n";
cout <<"2=to multiply\n";
cout <<"3=to add\n";
cout <<"4=to subtract\n";
cout <<"\n";

int number;
cout <<"Please enter the number that\n";
cout <<"will be used in this math problem\n";
cin >>number;
if (number==1)
{
int number1;
cout <<"Please enter the first variable\n";
cin >> number1;

int number2;
cout <<"Please enter the second variable\n";
cin >> number2;

int answer;
answer = number1 / number2;
cout <<"The answer is " <<answer;
}
else if (number==2)
(
int n1;
cout <<"Please enter the first variable\n";
cin >>n1;

int n2;
cout <<"Please enter the second variable\n";
cin >>n2;

int ans;
answer = n1 * n2;
cout <<"The answer is "<<ans;
}
else if (number==3)
{
int nu1;
cout <<"Please enter the first variable\n";
cin >>nu1;

int nu2;
cout <<"Please enter the second variable\n";
cin >>nu2;

int answ;
answer = nu1 + nu2;

cout <<"The answer is "<<answ;
}
else if (number==4)
{
int num1;
cout <<"Please enter the first variable\n";
cin >>num1;

int num2;
cout <<"Please enter the second variable\n";
cin >>num2;

int answ;
answer = num1 - num2;

cout <<"The answer is "<<answ;
}





getchar(); //Program will not exit until it gets a character
}


I cant figure it out!!

Visidex
01-20-02, 04:17 PM
You can use if statements as you did in first one, but an if-else or a switch would work better.

As for your code, you have a couple errors.
First:
else if (number==2)
(

The "(" should be a bracket thing "{".
That cut the errors I was getting from 17 to 1, but that is misleading.

The next part is a biggy. You have 3 variables declared in each if-else block. While that is fine, you may want to just declare them before going into the if-else. In every block after the first one, your logic is skewed.
Example:

int ans;
answer = n1 * n2;
cout <<"The answer is "<<ans;

You should see the error in that. It's also evident in 3rd and 4th one.

A note about variables:
You declared them in each block and with different names, which is fine but can get confusing. Since each piece of code is enclosed by "{ }", it has its own scope. Variables declared inside that are considered local variables and cannot be seen out side the brackets. So you could name each variable the same and be fine, or just declare them before the if-else and then they could be accessed from within the block.

If any of that doesn't make sense or you don't understand, I can provide code to go along with it.

Bmxpunk86pl
01-20-02, 09:52 PM
k thanks man

Bmxpunk86pl
01-20-02, 10:00 PM
ok i got it fixed but there still is an error, and i cant figure it out. (im new to C++)

//This is a Calculator made by Adam.
#include <iostream.h>
#include <stdio.h>



int main()
{
cout <<"****************************\n"; //Title of the program
cout <<"* CALCULATOR *\n";
cout <<"****************************\n";
cout <<"1=to divide\n";
cout <<"2=to multiply\n";
cout <<"3=to add\n";
cout <<"4=to subtract\n";
cout <<"\n";

int number;
cout <<"Please enter the number that\n";
cout <<"will be used in this math problem\n";
cin >>number;
if {number==1}
{
int number1;
cout <<"Please enter the first variable\n";
cin >> number1;

int number2;
cout <<"Please enter the second variable\n";
cin >> number2;

int answer;
answer = number1 / number2;
cout <<"The answer is " <<answer;
}
else if {number==2}
}
int n1;
cout <<"Please enter the first variable\n";
cin >>n1;

int n2;
cout <<"Please enter the second variable\n";
cin >>n2;

int ans;
ans = n1 * n2;
cout <<"The answer is "<<ans;
}
else if {number==3}
{
int nu1;
cout <<"Please enter the first variable\n";
cin >>nu1;

int nu2;
cout <<"Please enter the second variable\n";
cin >>nu2;

int answ;
answ = nu1 + nu2;

cout <<"The answer is "<<answ;
}
else if (number==4)
{
int num1;
cout <<"Please enter the first variable\n";
cin >>num1;

int num2;
cout <<"Please enter the second variable\n";
cin >>num2;

int answ;
answ = num1 - num2;

cout <<"The answer is "<<answ;
}





getchar(); //Program will not exit until it gets a character
}

Bmxpunk86pl
01-20-02, 10:02 PM
OK here is an update, but still i get errors



//This is a Calculator made by Adam.
#include <iostream.h>
#include <stdio.h>



int main()
{
cout <<"****************************\n"; //Title of the program
cout <<"* CALCULATOR *\n";
cout <<"****************************\n";
cout <<"1=to divide\n";
cout <<"2=to multiply\n";
cout <<"3=to add\n";
cout <<"4=to subtract\n";
cout <<"\n";

int number;
cout <<"Please enter the number that\n";
cout <<"will be used in this math problem\n";
cin >>number;
if {number==1}
{
int number1;
cout <<"Please enter the first variable\n";
cin >> number1;

int number2;
cout <<"Please enter the second variable\n";
cin >> number2;

int answer;
answer = number1 / number2;
cout <<"The answer is " <<answer;
}
else if {number==2}
}
int n1;
cout <<"Please enter the first variable\n";
cin >>n1;

int n2;
cout <<"Please enter the second variable\n";
cin >>n2;

int ans;
ans = n1 * n2;
cout <<"The answer is "<<ans;
}
else if {number==3}
{
int nu1;
cout <<"Please enter the first variable\n";
cin >>nu1;

int nu2;
cout <<"Please enter the second variable\n";
cin >>nu2;

int answ;
answ = nu1 + nu2;

cout <<"The answer is "<<answ;
}
else if {number==4}
{
int num1;
cout <<"Please enter the first variable\n";
cin >>num1;

int num2;
cout <<"Please enter the second variable\n";
cin >>num2;

int answ;
answ = num1 - num2;

cout <<"The answer is "<<answ;
}

Visidex
01-20-02, 10:48 PM
heh, didn't make myself clear the first time. Before when i said change the parentheses I meant the one on the second line, not all of them in the if statement. It should be:
if ()
{
}

Here's my fix, changes in bold:

//This is a Calculator made by Adam.
#include <iostream.h>
#include <stdio.h>



int main()
{
cout <<"****************************\n"; //Title of the program
cout <<"* CALCULATOR *\n";
cout <<"****************************\n";
cout <<"1=to divide\n";
cout <<"2=to multiply\n";
cout <<"3=to add\n";
cout <<"4=to subtract\n";
cout <<"\n";

int number;
cout <<"Please enter the number that\n";
cout <<"will be used in this math problem\n";
cin >>number;
if (number==1)
{
int number1;
cout <<"Please enter the first variable\n";
cin >> number1;

int number2;
cout <<"Please enter the second variable\n";
cin >> number2;

int answer;
answer = number1 / number2;
cout <<"The answer is " <<answer;
}
else if (number==2)
{
int n1;
cout <<"Please enter the first variable\n";
cin >>n1;

int n2;
cout <<"Please enter the second variable\n";
cin >>n2;

int ans;
ans = n1 * n2;
cout <<"The answer is "<<ans;
}
else if (number==3)
{
int nu1;
cout <<"Please enter the first variable\n";
cin >>nu1;

int nu2;
cout <<"Please enter the second variable\n";
cin >>nu2;

int answ;
answ = nu1 + nu2;

cout <<"The answer is "<<answ;
}
else if (number==4)
{
int num1;
cout <<"Please enter the first variable\n";
cin >>num1;

int num2;
cout <<"Please enter the second variable\n";
cin >>num2;

int answ;
answ = num1 - num2;

cout <<"The answer is "<<answ;
}


getchar(); //Program will not exit until it gets a character
}

_Will_
01-20-02, 10:54 PM
1) You're using {}'s instead of ()'s when you use if and else if statements like this:

if (x==0)
{
cout << " X is equal to zero";
cout << endl << "funky, eh?";
}

2) You're forgetting a few of the closing brackets (see above)

3) What compiler do you use?

the following

#include <iostream.h>
#include <stdio.h>

is the old way of including directives...from orignal C. Try the following format:

#include <iostream>
#include <stdio>
using namespace std;

XWRed1
01-20-02, 11:31 PM
Dude, just show us compilation or logic errors, don't paste all the code. Just say what you changed, and tell us what is still wrong.

_Will_
01-21-02, 01:00 AM
Originally posted by XWRed1
Dude, just show us compilation or logic errors, don't paste all the code. Just say what you changed, and tell us what is still wrong.

Yeah, unless you're using a sort of ghetto compiler it will tell you pretty straight-forwardly where the errors are and what is wrong with them. If you can't find anything wrong on that particular line its usually the one directly above it.

Bmxpunk86pl
01-21-02, 12:06 PM
k this is what i got:
line 22 c:\mydocu~1\calcul~1.cpp parse error before `{'


line 22 c:\mydocu~1\calcul~1.cpp confused by earlier errors, bailing out

XWRed1
01-21-02, 12:10 PM
Its because you're using { and } in the if statements, you should be using ( and ). Look at Visidex's post, he laid it out for you.

Bmxpunk86pl
01-21-02, 02:49 PM
Ok thanks for all your help everyone

BTW, i had a post a while back that asked for suggestions for programs. Well what i meant was programs like the one above that arent to complicated (for me, a newbie to C++) is there any suggestions to programs like this?

Cowboy Shane
01-23-02, 06:33 PM
The following is how I would have done your program. I am not trying to be critical, just give you some general ideas on c++ coding. I am sure my formatting will be killed, but this program does compile. It does not attempt to verify user input.

/*This is a cut down version of your program
the rest of the menu and the operations need to
be added, but I think you can see where I am going with this.
You just have a lot of redundant code
Hope this helps some*/

#include <iostream>
//No using statement here, I don't like polluting the namespace
//but it's your choice if you don't like the extra typing
int main ( void )
{
int num1, num2; //floats may be better
int choice; //I wouldn't use an int here
int answer;
std::cout << "Menu choices\n"; //std needed due to no using directive
std::cout << "1 -- Divide 2 numbers\n";
//Insert rest of menu

std::cout << "Enter your choice\n";
std::cin >> choice; //What happens if this is a char?

//Your program only allowed two operands
std::cout << "Enter a number\n";
std::cin >> num1;
std::cout << "Enter another number\n";
std::cin >> num2;

switch(choice)
{
case 1 : answer = num1 / num2;
break;
//Insert other choices here
default : std::cout << "Invalid choice\n"; //Minimal error checking
}
std::cout << "The answer is " << answer << std::endl;
return 0;
}

Guid0
01-27-02, 08:29 PM
What is the deal with the std:: stuff?
why is that the newer way?
i have been using <xxxx.h>
Is this a cross platform thing.
seems like more of a pain.
-----------------------------------------------------------------------------
also i don't know if anyone answered your original question about if statements.

yes you can use your if statemenst like this

if (x < y) swap(x,y); or whatever you want it to do.
if (x < z) swap(x,z);{use brackets to setup scope}
if (y < z) swap(y,z);

and tag on a "else" at the end if you want
psuedo:
if (num==1)
{
do this
}

if (num==2)
{
do this
}

else
{
do this
}

one thing the fellas didn't mention is Documentation. use it ........i took C++ intro my freshman year of college, and C++ advanced in senior year...try reading your own code after 3 years ro so and you'll wish you documented. it may seem a little trivial right now, but as you get better you programming will become more complex. plus if you wanna reuse your code it's nice to have spearted it.

although i do agree that case\switch is much cleaner.

hope i didn't make to many mistakes

Cowboy Shane
01-27-02, 09:58 PM
What is the deal with the std:: stuff?
why is that the newer way?
i have been using <xxxx.h>
Is this a cross platform thing.
seems like more of a pain.


I don't want to start a relligous war on C++ coding styles here (something that seems all to easy to do), I just wrote the code the way I always write code. Why use namespace scope resolution (ie std::cout)? Because in any non trivial application with a lot of header files and classes, some of wich may be precompiled, it's easier for me to declare my own namespaces so I don't have to worry if the length variable I declared locally conflicts with someone elses length variable elsewhere in the program. Most of my stuff is done in teams, so it's just easier for me to declare my own variables in a different namespace and specify which one I need at the time.

However, in the comments of the sample code I explicitly stated it was up to the programmer whether he or she wanted to put the directive #using namespace std; at the front of the program to save on typing. My personal taste is not to automatically declare all the variables stored in std unless I need them.

<doh.. it's late and I can't spell...>

Guid0
01-27-02, 11:47 PM
I was not trying to pravoke..........i honestly wanted you to tell why........i have never used the std:: and was not sure what or why use it.

i can see it's aplications though

XWRed1
01-28-02, 03:22 AM
Yea, its a namespace thing. Another way to do it is to put "namespace std;" at the beginning of your file.

Bmxpunk86pl
01-29-02, 05:52 PM
ok, u guys seem like u can help me. I have one more question, whats the #define thing used for? I know what the #include is but im not sure what the define thing is.
i appreciate any help

Visidex
01-29-02, 07:02 PM
It's used to define macros.
#define <macro name>(<parameters>) <code>

ex.
if you used this: #define MyMacro(text) printf(text);
calling this: MyMacro("Hello World!\n");
would be the same as: printf("Hello World!\n");

Bmxpunk86pl
01-29-02, 08:08 PM
ok the thanks, but can the #define thing be used in C++ or just in C?

Cowboy Shane
01-29-02, 08:41 PM
#define is more of a c thing, but it can be used in c++. I haven't seen the use of it for macros before, I've only seen it used to define constants like #define INT_MAX 32768 (the macro use seems a lot like ASM). Typically, in c++ I use inline functions if they are that short, or the const keyword for constants. The big benefit of #define seems to be that since it's a preprocessor command, the text in the program is replaced by the define text before the program is compiled which can speed the program up a bit as the computer doesn't have to jump to different points to look up a function or constant. I do believe, however, that inline functions have the same benefit.