Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Issue with simple number counting application
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sheepo39




PostPosted: Mon Dec 14, 2009 6:18 pm   Post subject: Issue with simple number counting application

I'm working on a number counting application and the code is

CODE:
/* A simple C++ application which asks the user
 for two numbers, then counts from the smaller number
 to the larger number */
#include <iostream>
#include <unistd.h>

using namespace std ;

int main()
{
        // Declares two variables of type int
        int num1 ;
        int num2 ;
        int lesserNumber ;
        int greaterNumber ;
        int numberOfCounts = 1 ;
       
        // Asks the user for two integer numbers
        cout << "Please enter an integer number: " ;
        cin >> num1 ;
        cout << "Please enter another integer number: " ;
        cin >> num2 ;
       
        // Checks to see which number is the lesser one, and which one is greater
        if ( num1 < num2 )
        {
                lesserNumber = num1 ;
                greaterNumber = num2 ;
        }
               
        else
        {
                lesserNumber = num2 ;
                greaterNumber = num1 ;
        }
       
        //Outputs the numbers from lesserNumber to greaterNumber
        for ( lesserNumber; lesserNumber <= greaterNumber ; lesserNumber++ )
        {
               
                // Checks if the number of counts is equal to 1
                if ( numberOfCounts == 1 )
                {
                        cout << "First number: " << lesserNumber << endl ;
                }
               
                else if ( numberOfCounts < greaterNumber - lesserNumber )
                {
                        cout << "Number: " << lesserNumber << endl ;
                }
               
                else if ( numberOfCounts == greaterNumber - lesserNumber )
                {
                        cout << "Final number: " << lesserNumber << endl ;
                }
               
                numberOfCounts++ ;
                sleep(1) ;
        }

        return 0 ;
}


Say, I choose one, and ten, The output is

First number: 1
Number: 2
Number: 3
Number: 4
Final number: 5

However, as you can see, it stops at five instead of ten, I don't get what is causing this. Any ideas on how to fix this?

Thanks,

John
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Dec 14, 2009 7:20 pm   Post subject: RE:Issue with simple number counting application

You're incrementing 'lesserNumber' in addition to 'numberOfCounts' (see your for() line...), which is what you seem to have intended. This makes the third condition in your loop true because ( 5 == 10 - 5) is true. Your loop line should declare and initialise numberOfCounts before the first semicolon; next it should check that numberOfCounts < greaterNumber; then it should numberOfCounts++ .
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: