if statement ,error!


how to solve this warning!?

2008\projects\lap7.ex3\lap7.ex3\ex3.cpp(34) : warning C4700: uninitialized local variable 'd' used
2008\projects\lap7.ex3\lap7.ex3\ex3.cpp(39) : warning C4700: uninitialized local variable 'q' used


the warning appears here!
else if (x==d)
{
divide = x1/x2;
cout << divide << endl;
}
else if (x==q)
cout << "Quit" << endl;
return 0;

}

help help:/
The problem is relatively straightforward. What is wrong with your program is you have not put any value into the variables d or q. That means they are uninitialized, and you cannot compare x to something that doesn't have any value in it. The statements if(x==d) and if(x==q) therefore are in fault.
Last edited on
you cannot compare x to something that doesn't have any value in it

If only that were the case. But the thing is, there IS a value at that point. However, it might be any kind of random trash value that occupied the memory the variable refers to before.

But there is no value for d and q !
if I say that q = "Quit" as a value , it will give me another error !!
what kind of values do you mean?
"But there is no value for d and q !" I think that can not be the case. Variables must have values before using them.
I think you post your complete main function, people can figure out it more clearly.

here is the problem:
Write a C++ calculator that takes two numbers and a letter indicating an
operation to be performed. The user enters the letter and the program performs the
operation depending on the letter as follows:
A: Add (the two numbers)
S: Subtract
M: Multiply
D: Divide
Q: Quit (exit the program).



and here is my answer:
#include<iostream>

using namespace std;

int main()
{

int x1,x2;
char x;
char a,s,m,d,q;
int add,subt,multip,divide;
cout << "Enter the tow numbers: " << endl;
cin >> x1 >> x2;

cout << "Enter the letter : " << endl;
cin >> x;

if (x==a)
{ add = x1+x2;

cout << add << endl;
}
else
if (x==s)
{ subt = x1-x2;
cout << subt<< endl;
}
else if (x==m)
{
multip= x1*x2;
cout << multip << endl;
}

else if (x==d)
{
divide = x1/x2;
cout << divide << endl;
}
else if (x==q)
cout << "Quit" << endl;
return 0;

}
This is my opinion:

I don't have experience with C++ (I used fortran mostly), it is better to use the "switch case" control, but since you used "if", you can stick with that. But the "else" statement is completely unnecessary, if not wrong, I would remove them. (If the "IF" doesn't meet, it just skips {} to go to the next statement.)

Secondly, you have to initiate value for a,s,m,d,q. I think you have problem with distinguishing the names of variables with their values, do a = "a", s ="s"... to see how it goes. If it works then I am right, and you can give some minutes to think about that.

And if I were you I would put all the semicolon at the end of a statement, including };. That builds up the concept of statements and block of statements more clearly.

I did as what you said I put a value for each char , a="a" ....

but I got this error:
error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char'

What to do ?

I made a silly mistake a = 'a' NOT "a" :b

It works ,,

But , how to make the program repeat asking about more other two numbers? (repetition),
until I press (q) to Quit
Sorry, it was my fault using "a", it should be 'a' in C++.
Use:
char a = 'a', s = 's', m = 'm', d = 'd', q = 'q';
I checked your code, with "else"s removed, it ran (but didn't check if it is correct.)
so put a='a', d='d' etc. (single quotes) like you would also have to do in C...

cheers,......
thanks it works :>

But, how to make the program repeat asking about more other two numbers? (repetition),
until I press (q) to Quit
Have a look at "The do-while loop," which repeats actions until you don't want it to continue.
http://www.cplusplus.com/doc/tutorial/control/
Best.
Topic archived. No new replies allowed.