Need help with a loop program

This is a discussion on Need help with a loop program within the C++ Programming forums, part of the General Programming Boards category; hey, im writing a program that requires me to have the user type in random numbers between 0 and 9999, ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    Need help with a loop program

    hey,
    im writing a program that requires me to have the user type in random numbers between 0 and 9999, and my program should keep track of all the numbers. when the user types -1, the program should show the maximum and minimum number they entered. how do i keep track of all the numbers the user enters and how do i show the maximum and minimum number of those set of numbers? (it's supposed to be a loop program)
    thanks!

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I take it you know how to loop? Why not use a vector of integers and each time push back untill they type -1.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Vectors have no use for this task.
    You only need to keep track of 2 number.
    Code:
    int max = 0;
    int min = 0;
    
    void AddNumberFromUser(int num)
    {
       if(num > 0)
       {
          if(num > max)
             max = num;
          if(num < min)
             min = num;
       }
       else
       {
          cout << "The largest user input is " << max << ", and the smallest is" << min << ".";
       }
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
    	int number,max=0,min=9999;
    	vector<int> randomNumbers;
    	cout << "Type in random numbers 0 - 9999. Enter -1 to stop.\n";
    	do{
    		cout << "Number: ";
    		cin  >> number;
    
    		if(number>=0)
    		{
    			randomNumbers.push_back(number);
    			if(number<min)
    				min=number;
    			if(number>max)
    				max=number;
    		}
    	}while(number>=0);
    
    	if(randomNumbers.size()>0)
    	{
    		cout << "\nYou entered the following numbers:\n";
    		for(int i=0;i<randomNumbers.size();i++)
    			cout << randomNumbers[i] << endl;
    		cout << "\nThe min was " << min << " and the max was " << max << ".\n";
    	}
    	else
    	{
    		cout << "You didn't enter any numbers.\n";
    	}
    
    	return 0;
    }
    this will ask them to enter numbers until then enter a negative number, then display all the numbers they picked, the min, and the max. If you don't want to list all the numbers they picked then you don't need the vector or the for loop(or the cout right before the for loop).
    Your Welcome

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    thanks guys but your methods are advanced for me. I'm very new to programming and hardly know what you guys are talkin about. Anyway, i made a code for the program i'm making, but whenever i run the program and it finds the min., the min. is -1. It finds the max correctly, but the min is always -1. Can someone help me?
    Last edited by hieugene; 10-09-2006 at 03:46 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Sorry guys

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
    	char choice;
    	double number=0, min=number, max=number;
    
    
    	do {
    		cout << "Enter as many numbers as you want. Enter -1 to stop." << endl;
    
    
    		do {
    		cout << "Enter a number: ";
    		cin >> number;
    
    		if ( number < -1 || number > 9999 ) {
    			cout << "Only enter numbers greater than -1 and less than 9999" << endl;
    		}
    
    		else if ( number > max ) {
    		max = number;
    		}
    		else if ( number < min ) {
    		min = number;
    		}
    
    		} while (number != -1);
    
    		cout << "Min = " << min << endl;
    		cout << "Max = " << max << endl;
    
    		cout << "Continue? (y or n)";
    		cin >> choice;
    
    
    	} while ( choice == 'y');
    	
    		if (choice == 'n') {
    		cout << "End program." << endl;
    		}
    }

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So you are using Queatix's idea? His code has one very obvious bug and you are making the same mistake. It's min you are having trouble with. Can you spot the error?

    If you fix that, min would still print -1. You should not be processing the last input.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    can someone just tell me the answer? a dry answer....im so confused with what to do to fix this program.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Quote Originally Posted by hieugene
    Sorry guys

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
    	char choice;
    	double number=0, min=number, max=number;
    
    
    	do {
    		cout << "Enter as many numbers as you want. Enter -1 to stop." << endl;
    
    
    		do {
    		cout << "Enter a number: ";
    		cin >> number;
    
    		if ( number < -1 || number > 9999 ) {
    			cout << "Only enter numbers greater than -1 and less than 9999" << endl;
    		}
    
    		else if ( number > max ) {
    		max = number;
    		}
    		else if ( number < min ) {
    		min = number;
    		}
    
    		} while (number != -1);
    
    		cout << "Min = " << min << endl;
    		cout << "Max = " << max << endl;
    
    		cout << "Continue? (y or n)";
    		cin >> choice;
    
    
    	} while ( choice == 'y');
    	
    		if (choice == 'n') {
    		cout << "End program." << endl;
    		}
    }
    Code:
    double number, min=9999, max=0;
    don't need to give number a initial value, thats what the cin is for(though you can if you want too). The min must be set as 9999, otherwise it will always stay at 0.

    Code:
    else if ( number > max ) {
         max = number;
    }
    else if ( number < min ) {
         min = number;
    }
    don't use the else if's. just do one else and check for the max and min under it. You need to check for both because if the user only enters 1 number, then it would have only affected the max, skipping right over the min.

    Code:
    else
    {
              if(number<min)
              {
                        min=number;
              }
              if(number>max)
              {
                        max=number;
              }
    }
    Code:
    if ( number < -1 || number > 9999 ) {
              cout << "Only enter numbers greater than -1 and less than 9999" << endl;
    }
    should be number < 0 so that when the user enters -1 it doesn't change the min. Also, if you don't want the message about only entering number greater than -1 and less than 9999 to come up when -1 is entered, put in another if else.

    Code:
    if ( number < 0 || number > 9999 )           
    {          
              if(number=-1)
              {
                        break;
              }
              else
              {
                        cout << "Only enter numbers greater than -1 and less than 9999" << endl;
              }
    }
    hopefully that will answer all your questions
    Last edited by JukeBoxHero; 10-09-2006 at 09:03 PM.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    how do i keep track of all the numbers the user enters
    including the numbers under 0 and over 9999?

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void A();
    
    int num = 0;
    int min = 0;
    int max = 0;
    
    vector<int> a;
    
    int main()
    {
        for (;;)
        {
            cin>>num;
            A();
        }
        return 0;
    }
    
    void A()
    {
         if (num == -1)
         {
            cout<<"min: "<<min
                <<endl
                <<"max: "<<max;
            return;
         }
         if (num < 0)
            return;
         if (num > 9999)
            return;
         if (num < min)
            min = num;
         if (num > max)
            max = num;
         a.push_back(num);
    }
    havent tested it tho

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    What makes you think that you need to keep track of all numbers entered?

  12. #12
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Quote Originally Posted by iMalc
    What makes you think that you need to keep track of all numbers entered?
    u talking to me, hiugene or who?

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You. Drop the vector. All you need is a max and min.

    Ok, step by step...

    Code:
    set max to the first input number, or to a very low number (-1 is low and invalid so we can use it)
    
    user inputs 10
    we know that no matter whatever else the user inputs the max is at least 10, so max is set to 10
    
    user inputs 8
    max cannot be 8 (because there is a 10), so max is unchanged
    
    user inputs 17
    set max to 17
    
    user inputs 16
    leave max unchanged
    
    user inputs -1
    done, the max is 17
    For min, just invert the comparisons. Unless you want to set a lower and/or upper limit on user input, you should set max and min to the first input number.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, program stuck in a while loop
    By DSman in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 02:28 PM
  2. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 04:30 AM
  3. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 07:55 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM

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