I have to write a program that randomly generates a number from 1 to 10 and tells you to guess the number. when you guess it tells you if you're right or wrong and if you are right then it ends but when your wrong it is supposed to say try again and loop back until you get it right,
Put the code you need help with here.
int main() {
int Secret=1, Guess=1;
srand(Secret>0<10);
printf("\nGuess a Number from 1 to 10");
scanf("%d", &Guess);
if Guess=Secret printf("\nCongratulations that is the secret number!");
if Guess!=Secret printf("\nGuess again please!");
if Guess!=Secret return 14;
return 0;
}]
I was trying to figure out how to paste this in the proper format I'm not sure how. but the 3 lines that start with "if" all tell me this "[Error] expected '(' before 'Guess'." idk if I'm even on the right track. idk if I used the srand() function properly. I cant really figure out tho because I cant get past this issue with the if's to fiddle more. any help would be greatly appreciated
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
/* Joshua M. Spalding */
/*CECS 130-50 */
/* Assignment 2 problem 4*/
int main() {
int Secret=1, Guess=1;
srand(Secret>0);
printf("\nGuess a Number from 1 to 10");
scanf("%d", &Guess);
if (Guess==Secret)
cout<<"\nCongratulations that is the secret number!";
if (Guess!=Secret)
cout<<"\nGuess again please!";
if (Guess!=Secret) return 14;
return 0;
sry still not sure how to copy in the proper format. But now it is telling me that cout was not declared in this scope for the two lines with cout. feels like I'm getting closer but I'm stuck again I thought # include <iostream> would fix this issue, but it didn't, am I misunderstanding something?
the headers thing I think is messing with me, gonna have to make some flash cards with those. I think I got it now, and much appreciated. and sry I try not to spam questions on here I read the intro post, I just had a deadline, which is my fault I know. I was however looking for an answer and fiddling with the text constantly. so I'm not just trying to be lazy is all I'm saying. but thanks again
When it tells you that cout was not declared, it is because cout is not a global function, parameter or other kind of thing you can do. For example, this program will not work:
Note that in the above one, it uses std:: as a prefix, this means that you are looking for a function in the std class. using the command usingnamespace std automatically adds an imaginary std:: to all the commands that you call after that, that use std:: as their prefix.
also, to properly format your code to look like this type ["code"] ;; ["/code"] where " is removed, and ;; is just your code.
As to why your code isn't working, try an approach more like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main(){
start:
int guessed = 0, generated = rand() % 10;
cout << endl << endl << "Guess a number from 1 to 10" << endl << endl;
cin >> guessed;
if(generated == guessed){
cout << endl << endl << "Congratulations, that is the secret number";
}
else{
cout << endl << endl << "Guess again please";
goto start;
}
return 0;
}