program not working, suggestions?

hello im trying to create a program in which the user has to guess which number the computer picked from a number between 0-10, if the user got it correct the the program displays "Good job" and if not the program displays "0w no, not quite". I recommended that u put the code into your compiler, because there are quite a few error i know this seems like a trivial program, but i need it for a much bigger project im working on.

Thanks in advanced for any helpful responses.
Daniel.B


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int rand_0toN1(int n);

     
int main(){
    
    int no_numbers[10] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };
    
    int a;
    int b;

    srand(time(NULL));

   
    cout << "Enter a number: ";
    cin >> a;
    
If (a == rand_0toN1(int n)) 
       cout << "Good job";
       else
       cout << " 0w no, not quite";
       
     return 0;
     }
     
    
int rand_0toN1(int n){
    return rand(10) % a;
}

http://www.cplusplus.com/forum/articles/6046/
Read that :) It'll tell you how to read input from the user correctly.

2nd:
Line 23: it's if not If. Case-sensitive
Line 33: a is not defined within the scope of that function.
Line 32: What is the int n for?
Line 23: Your not passing in any n, but a declaration. Cannot do this.

Write your program out in english step by step, then code it that way.

e.g
Seed random number generator
generate random number between 1-10 etc
I'm still new myself and about to post for some help to lol, but after reading your code first thing that came to my mind was what is N?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int a;
int rand_0toN1(int n);

     
int main(){
    
    int no_numbers[10] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };
    
  
    int b;

    srand(time(NULL));

   
    cout << "Enter a number: ";
    cin >> a;
    
if (a == rand_0toN1(int n)) 
       cout << "Good job";
       else
       cout << " 0w no, not quite";
       
     return 0;
     }
     
    
int rand_0toN1(int n){
    return rand(10) % a;
}




sorry i fixed up the stupid stuff, but i still dont understand line 23, and 32 what would i put just int rand_0toN1();


could you give me an example code or something.


Last edited on
Re-read my post, and map our in English what you want to do. You've got some design flaws in your application currently that will prevent it from functioning correctly. Once you have a list of things to achieve in order then you can progress.
well for a start i dont understand rand_0toN1 function, you you please explain it or could you give me a url to one that will

thanks for you help so far
That function makes no sense in the context of your application.

It is returning the result as the remainder of a random number divided by a (undefined). You don't need this function for your application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int a;
int rand_0toN1(int n);
int n;

     
int main(){
    
    int no_numbers[10] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };
    
  
    int b;

    srand(time(NULL));

   
    cout << "Enter a number: ";
    cin >> a;
    
if (a == rand_0toN1( n)) 
       cout << "Good job";
       else
       cout << " 0w no, not quite";
       
     return 0;
     }
     
    
int rand_0toN1(int n){
    return rand()% a;
}




so is this correct
No. Your not reading my replies properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main() {
  
  srand(time(NULL));
  int numberToGuess = rand() % 10;
  
  int userGuess = 0;
  cout << "Enter guess: ";
  cin >> userGuess; // WRONG way to do this, see my previous link
  
  //if (userGuess == numberToGuess)
    // etc
    
  return 0; 
}

Last edited on
ok thank you, could you please explain
int numberToGuess = rand() % 10;
in the example above.

i thought u had to use rand_0toN1 for random numbers.
rand_0toN1 was a user defined function.

rand() return a random number. Don't remember the range.
% 10 gets the remainder of that number when divided by 10, so in this case the number would be 0-9.
understand, thank you for my stupidity before i was trying to use another example to help me, for a book which must have confused me alot.

once again thank you.
Topic archived. No new replies allowed.