Thread: NEED HELP! Writing an elementary C++ program

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    Question NEED HELP! Writing an elementary C++ program

    Hey everyone I am having a really hard time trying to figure out what I am doing wrong in this program we are writing for class. The assignment is to use the for function to write a code that will first let the user choose how many iterations are run and second tell the user what the lowest number that was entered was. (The latter is where I am having some troubles.) I cannot seem to figure out how to get the program to store the previous lowest number for the life of me. I have tried everything I can think of. If anyone could help me out it would be much appreciated.

    Here is my code:

    Code:
    #include <iostream>#include <iomanip>
    using namespace std;
    
    
    int main()
    {
        int MinNum = 0, 
            MaxNum = 1,
            num2;
    
    
        cout << "How many numbers would you like to enter? ";
        cin >> MaxNum;
        cout << endl;
    
    
        for (int num = 1; num <= MaxNum; num++)
        {
            cout << "Enter a number: ";
            cin >> num2;
            if (!( num2 < MinNum))
                MinNum = num2;
        }
    
    
        cout << endl;
        cout << "Here is the lowest number you entered: " << MinNum << endl;
        cout << endl;
    
    
        system("pause");
        return (0);        
    }

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Code:
    if (!( num2 < MinNum)) // if(NOT(num2 smaller than current minimum))
        MinNum = num2;
    But you'd want to do something if num2 is smaller than prior values, not if it isn't. And give your variables better names; num, num2, MinNum and MaxNum do pretty different things in your program, so their names shouldn't be variations of one another.

    p.s. think about whether 0 is a good choice for MinNum's initial value. Consider different scenarios of user input.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    Still not running correctly...

    Unfortunately, this just gives me the largest value entered... And it doesn't seem to change whether I put in an or statement or
    the sign...

    Code:
    #include <iostream>#include <iomanip>
    using namespace std;
    
    
    int main()
    {
    	int MinNum, 
    		Counter,
    		small;
    
    
    	cout << "How many numbers would you like to enter? ";
    	cin >> Counter;
    	cout << endl;
    
    
    	for (int num = 1; num < Counter; num++)
    	{
    		cout << "Enter a number: ";
    		cin >> small;
    		if ((small < MinNum) && (!(small > MinNum)))
    			MinNum = small;
    	}
    
    
    	cout << endl;
    	cout << "Here is the lowest number you entered: " << MinNum << endl;
    	cout << endl;
    
    
    	system("pause");
    	return (0);		
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    Always initialize your variables. I would prefer to declare your variables only when you're ready to initialize and or use them, which also helps remind you of that rule. That might help you here.

    You also might want to think about how many times your for loop runs, but that's probably not your current problem. Basically, you've made a couple changes from your original attempt that made things worse.

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    Thanks. I went back to my original code and I figured it out. I was trying to over-complicate it for sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Elementary question
    By Dan. in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2011, 01:47 PM
  2. Very elementary C++ question
    By Dondrei in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2008, 09:00 AM
  3. elementary networking problem
    By nash@nitk in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-26-2006, 10:21 PM
  4. An Elementary Windows Question
    By samGwilliam in forum Windows Programming
    Replies: 7
    Last Post: 01-20-2006, 04:20 PM
  5. elementary question
    By misplaced in forum Tech Board
    Replies: 8
    Last Post: 12-17-2004, 07:27 AM

Tags for this Thread