Question about count/for loop

Question:

The user is prompted to enter number of times to repeat the generation of the two numbers.Thetworandomnumbersarebetween0and100. Thethreepossiblescenarios are: the first number could be greater than the second number, less than the second number or equal to the second number. The percentage of occurrences for the three described scenarios are computed and displayed with 2 decimal places. The formula used is : x/count * 100 where x is the total number of times the first number is greater than/less than/equal to the second number. count is the number of times to repeat the loop.



The final output screen will be: (random numbers)

Enter the number of times to play: 100
First Number > Second Number percentage score: 44.00
First Number < Second Number percentage score: 54.00
First Number equal Second Number percentage score: 2.00
Press any key to continue...




Here is my code, and i think error starts from for loop but i dunno how to correct them... Pls help me!


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	// declare all variables

	int times, num1, num2, reply, count, percent1, percent2, percent3;

	// Prompt user to enter the number of times to play

	cout<<"Enter the number of times to play: ";
	cin>>times;

	// read times using for loop

	for (int count=0; count<times; count++)
	{
		num1 = rand()%100+1;   // generate a number between 1 and 100
		num2 = rand()%100+1;
	}

	if (num1>num2)    // print percentage score if num1>num2
	{
		percent1 = times / count * 100; → I don’t know how to compute the given formula x/count*100

		cout<<"First number > Second Number percentage score: "<< percent1 <<endl;
		cout<<fixed<<setprecision(2)
	}
	else 
		if (num1<num2)    // print percentage score if num1<num2
		{
			percent2 = times / count * 100;
			cout<<"First number < Second Number percentage score: "<< percent2 <<endl;
			cout<<fixed<<setprecision(2)
		}
		else    // print percentage score if num1=num2
		{
			percent3 = times / count * 100; 
			cout<<"First number equal Second Number percentage score: "<< percent3 <<endl;
			cout<<fixed<<setprecision(2)
		}


		// print 'press any key to continue'

		cout<<"Press any key to continue..."<<endl;
		cin<<reply;

	}
	cin.ignore();
	cin.ignore();
}
your code that is in the lines 24 - 53 should be inside the for loop.
I think there are more problems than that... The main problem I'm having is i dunno how to indicate the "x" given in the question --> the total number of times the first number is greater than/less than/equal to the second number...
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// there is still something wrong

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	// declare all variables

	int times, num1, num2, reply, count, percent1, percent2, percent3;

	// Prompt user to enter the number of times to play

	cout<<"Enter the number of times to play: ";
	cin>>times;

	// read times using for loop

	for (int count=0; count<times; count++)
	{
		num1 = rand()%100+1;   // generate a number between 1 and 100
		num2 = rand()%100+1;
	}

	if (num1>num2)    // print percentage score if num1>num2
	{
		percent1 = times / count * 100; // I don’t know how to compute the given formula x/count*100

		cout<<"First number > Second Number percentage score: "<< percent1 <<endl;
		cout<<fixed<<setprecision(2);
	}
	else 
		if (num1<num2)    // print percentage score if num1<num2
		{
			percent2 = times / count * 100;
			cout<<"First number < Second Number percentage score: "<< percent2 <<endl;
			cout<<fixed<<setprecision(2);
		}
		else    // print percentage score if num1=num2
		{
			percent3 = times / count * 100; 
			cout<<"First number equal Second Number percentage score: "<< percent3 <<endl;
			cout<<fixed<<setprecision(2);
		}


		// print 'press any key to continue'

		cout<<"Press any key to continue..."<<endl;
		cin>>reply;
}
Last edited on
You're attempting to use the same variable for all three outcomes. You also need to increment count for every time a given scenario is seen. And as stated above you need to include lines 23-46 in your for statement.
Last edited on
Thanks...and i have changed to initialise all counters to 0 like this:

int count_greater = 0;
int count_lesser = 0;
int count_equal = 0;

but i still can't figure out how to code this increment part before computing the percentage:

// if num1>num2 then increment greater
// if num1<num2 then increment lesser
// if num1=num2 then increment equal
You should add a counter for num1 being greater a counter for num2 being greater a counter for them being equal. Then, use a variable where you store the percentage
sorry i'm very new to c++ and i still can't figure out the correct way.. :(

do you mean this?

int count_num1greater = 0;
int count_num1lesser = 0;
int count_equal = 0;

could you please give an example how to code below pseudocode?

// if num1>num2 then increment greater
// if num1<num2 then increment lesser
// if num1=num2 then increment equal
Pls help :_(
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 <cstdlib>
#include <ctime>
#include <iomanip>

int main ()
{
    // assume that using <random> is not an option
    std::srand( std::time(nullptr) ) ; // seed the legacy rng
    const int ubound = 101 ; // upper bound on the numbers

    // Prompt user to enter the number of times to play
    std::cout << "Enter the number of times to play: ";
    int times ;
    std::cin >> times;

    int cnt_less = 0 ; // count of first number < second number
    int cnt_equal = 0 ; // count of first number == second number

    for( int count = 0 ; count < times ; ++count )
    {
        // generate two random numbers in the range [0,100]
        const int first = std::rand() % ubound ;  // not the best way of doing this
        const int second = std::rand() % ubound ;

        if( first < second ) ++cnt_less ;
        else if( first == second ) ++cnt_equal ;
    }

    const int cnt_greater = times - cnt_less - cnt_equal ; // count of first number > second number

    if( times > 0 ) // print out percentages
    {
        std::cout << std::fixed << std::setprecision(2) // fixed point, two digits after the decimal point
                  << "\n   first number less than second number: " << ( cnt_less * 100.0 ) / times << "%\n"
                  << "    first number equal to second number: " << ( cnt_equal * 100.0 ) / times << "%\n"
                  << "first number greater than second number: " << ( cnt_greater * 100.0 ) / times << "%\n" ;
    }
}

http://coliru.stacked-crooked.com/a/c6bdf8aaa0253e96
Hi JLBorges, i have input all the code into c++ but the program exit automatically after i key in any character when it prompts "Enter the number of times to play: ".......
Add this, right at the end (after line 38).
1
2
3
std::cout << "enter a (non-space) character: " ;
char some_char ;
std::cin >> some_char ;
JKBorges Really appreciate your help!

but, i have another question: I have never learnt std:: in school yet, is it really necessary to insert std:: in front of cout/cin,etc? Is there any other function can replace it for the same output?
There is using namespace std; that you can put above the main functin. But you're much better off doing what @JLBorges does and use std::, saves your ass in the future

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
> is it really necessary to insert std:: in front of cout/cin,etc?

See: http://www.cplusplus.com/forum/beginner/142171/#msg750694

Also see: http://www.cplusplus.com/forum/general/72248/#msg385442
Thank you both TarikNeaj and JLBorges so much for your time and help, much appreciated!
Topic archived. No new replies allowed.