using radom number with if else if else

This is a discussion on using radom number with if else if else within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<cmath> using namespace std; int main() { int answer = rand; int rand; cout << "How many gumballs ...

  1. #1
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18

    using radom number with if else if else

    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
    	int answer = rand;
    	int rand;
    	cout << "How many gumballs are in the machine.\n";
    	do{
    		cin >> answer;
    		if (answer < rand)
    		{ 
    			cout << "To low" << endl;
    		}
    		else if (answer > rand)
    		{
    			cout << "To high" << endl;
    		}
    		else
    		{
    			cout << "Good job now u can eat them.\n";
    		}
    	}while (answer != rand);
    	system("pause");
    	return 0;
    }
    error:
    1>c:\users\matthew\desktop\if, else if, else, with do while loop\if, else if, else, with do while loop\gumballs.cpp(6) : error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(void)' to 'int'

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Do you know how to call a function? rand is a function (which you should not reuse as a variable name). And you should seed the random number generator at the start of your program, before you generate any numbers. rand(3): pseudo-random number generator - Linux man page

  3. #3
    Registered User arc_angel14's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    18
    ok i fixed it but how do i get it to clse?
    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
    	int answer = rand();
    	cout << "How many gumballs are in the machine.\n";
    	do{
    		cin >> answer;
    		if (answer < rand())
    		{ 
    			cout << "To low" << endl;
    		}
    		else if (answer > rand())
    		{
    			cout << "To high" << endl;
    		}
    		else
    		{
    			cout << "Good job now u can eat them.\n";
    		}
    	}while (answer != rand());
    	system("pause");
    	return 0;
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Work on the logic on paper first. You're not even close at the moment. Search the forum for examples on how to use random numbers as well....this is a frequently discussed subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21