Reputation: 97
What am I doing wrong with the syntax of these If/Else statements I'm pretty sure it has something to do wiht my curly bracket placement. All of the examples in my book only contain really primitive/simple examples (not to say that this code isn't simple; b/c it is). I guess my general question would also be how do you separate these two separate statements. When I execute it seems that the two separate functions are blending together.
//
tens = rand / 10;
if (tens = 2){
cout << "twenty ";
else if (tens = 3)
cout << "thirty ";
else if (tens = 4)
cout << "forty ";
else if (tens = 5)
cout << "fifty ";
else if (tens = 6)
cout << "sixty ";
else if (tens = 7)
cout << "seventy ";
else if (tens = 8)
cout << "eighty ";
else if (tens = 9)
cout << "ninety ";
}
//
ones = rand % 10;
if (ones = 0){
cout << "\n";
else if (ones = 1)
cout << "one\n";
else if (ones = 2)
cout << "two\n";
else if (ones = 3)
cout << "three\n";
else if (ones = 4)
cout << "four\n";
else if (ones = 5)
cout << "five\n";
else if (ones = 6)
cout << "six\n";
else if (ones = 7)
cout << "seven\n";
else if (ones = 8)
cout << "eight\n";
else if (ones = 9)
cout << "nine\n";
}
Upvotes: 0
Views: 1988
Reputation: 3
You have to close the curly braces after each and every if or else if clause like
tens = rand / 10;
if (tens = 2) {
cout << "twenty "; }
else if (tens = 3) {
cout << "thirty ";}
else if (tens = 4)
cout << "forty "; }
else if (tens = 5) {
cout << "fifty "; }
else if (tens = 6) {
cout << "sixty "; }
else if (tens = 7) {
cout << "seventy "; }
else if (tens = 8) {
cout << "eighty "; }
else if (tens = 9) {
cout << "ninety "; }
//
ones = rand % 10;
if (ones = 0) {
cout << "\n"; }
else if (ones = 1) {
cout << "one\n"; }
else if (ones = 2) {
cout << "two\n"; }
else if (ones = 3) {
cout << "three\n"; }
else if (ones = 4) {
cout << "four\n"; }
else if (ones = 5) {
cout << "five\n"; }
else if (ones = 6) {
cout << "six\n"; }
else if (ones = 7) {
cout << "seven\n"; }
else if (ones = 8) {
cout << "eight\n"; }
else if (ones = 9) {
cout << "nine\n"; }
Upvotes: 0
Reputation: 361692
I think, for this particular problem if-else isn't even required!
I would rather recommend this solution:
const char *stens[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninty"};
const char *sones[] = {"", "one", "two", "three", "four", "five",
"six", "seven", "eigth", "nine"};
//make sure 0<= rand <= 99
cout << stens[ rand / 10 ] << " " << sones[ rand % 10 ] << endl;
Online Demo : http://www.ideone.com/K7HxS
As for the problem you're facing with if-else, you're using assignment operator, rather than equality, as everyone already have pointed out.
Upvotes: 6
Reputation: 4678
The assignment operator = will generally always return true as it returns the success of an assignment of value. The equality operator == is what your after. It will test the equivalency of two values.
In addition the if statement is either followed by a single statement or code block. In your example you lack the terminating brackets.
// This is the assignment operator with a single statement.
if ( a = b )
cout << "I am always true!" << endl;
// This is the equality operator with a code block.
if ( a == b ) {
cout << "I am sometimes true!" << endl;
} else {
cout << "I am more likely to be true." << endl;
}
Upvotes: 0
Reputation: 14873
why dont you use SWITCH CASE , think you have to case calculated value into int ie
ones = int (rand % 10);
ones = int (rand / 10);
example of switch case
switch (x) {
case 1:
cout << "x is 1";
break;
case 2:
cout << "x is 2";
break;
default:
cout << "value of x unknown";
}
Upvotes: 1
Reputation: 81724
ALso, the brace right after your "if" should be matched by one right before the first "else"
if (something) {
something
} else if (something) {
something
}
Upvotes: 0
Reputation: 10848
Yes, you are using the curly brace in a wrong way. Do it like this:
if (tens == 2)
{
// your commands
}
else
{
// your commands
}
And please use "==" operator for equal comparing, not "=".
Upvotes: 2
Reputation: 77094
A single equals sign is an assignment, rather than a test for equality.
You should be using ones == N
for some number N
.
It also seems like you're not using the braces ({
and }
) correctly, your if
statement ought to look like this:
tens = rand / 10;
if (tens == 2){
cout << "twenty ";
}else if (tens == 3){
Note the brace preceding the else
and after the 3)
.
Also, I'd recommend using a switch
statement in this scenario, it might lead to some easier to read and more manageable code:
tens = rand / 10;
switch(tens){
case 2: cout << "twenty "; break;
case 3: cout << "thirty "; break;
case 4: cout << "fourty "; break;
// ...
case 9: cout << "ninety "; break;
}
Upvotes: 5
Reputation: 35477
use "==" instead of "='. "=" is assignment operator; whereas "==" logical equals.
Upvotes: 3