0
What am I missing?
I'm trying to make a single die in C++ using What exactly is wrong with my code? I'm still new to C++ just trying to learn! Thanks! #include <iostream> using namespace std; int main() { cout << "Your " << "Number " << "is" << endl; int x=1; while(x>6);{x++;} if(x=1);{cout << "1";} else(x=2);{cout << "2";} else(x=3);{cout << "3";} else(x=4);{cout << "4";} else(x=5);{cout << "5";} else(x=6);{cout << "6";} return 0; }
4 Respostas
+ 4
Not trying to sound offensive or anything but you can better ask what is right. :)
It'll be a shorter answer.
Don't use ; after while or if, it'll end it.
if(x=1) is an assignment, you want to compare if(x==1)
else doesn't accept parameters, you probably mean else if(x == 2)
you can't stack else's like that either.
the while loop (assuming it was correct) is never entered because 1 > 6 = false anyway
and if the condition was true somehow you'd still not end up with a correct dice
This is all you need to generate a dice roll:
#include <iostream>
#include <ctime>
#include <cstdlib>
int main()
{
srand(time(nullptr));
int dice = rand() % 6 + 1;
std::cout << dice << std::endl;
return 0;
}
+ 2
x is never greater than 6 so it will never be incremented. It will stay 1. Try while x < 7. Also the syntax is a little ropey. Take another look at the positioning of your curly brackets, and also the number of ;'s (most of the code should be inside the while block)
It seems like you should be using a for loop here too. Do you know how to use those?
I can tell you the answer but i figures you'd want to try and solve it yourself first
+ 2
Dennis is absolutely right though I'd assume that will probably confuse you. I'm guessing being a newbie, and also my assumption was that you were trying to just print 1 2 3 4 5 6 on the screen. If that is the case use a for loop. If not use Dennis's and try ur hardest to understand / take a look at the libraries he's using and what the code he's using is doing
+ 1
Thanks Everyone! I appreciate your answers!😀😀