GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Sep-2008, 19:50
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Printing a range of inputted values?


Hi,

I'm trying to get my program to print out a specific output of numbers that fall into a range of what the user inputs, It works well for one at a time, but I need to get all the numbers to print at once once the user forces the program to stop.

This is what I have:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main (){
    
    int nums=0;
    int loop=0;
    
    cout << "Input positive values, input 0 when you wish to stop." << endl;
    cin >> nums;
    
    while ( nums != 0 ){
          cin >> nums;
          if ( nums >= 50 && nums <= 75)
    
          
          cout << "The range:" << nums << endl;

    }               
    system("PAUSE");
    
    return 0;
    
}
  #2  
Old 14-Sep-2008, 20:53
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Printing a range of inputted values?


Quote:
Originally Posted by 8100 Power
I'm trying to get my program to print out a specific output of numbers that fall into a range of what the user inputs, It works well for one at a time, but I need to get all the numbers to print at once once the user forces the program to stop.
It is unclear what you expect as output.

If during the processing of your while loop, I input the values 51, 52, 53, 0, what should be displayed? Is this to happen within the while loop or after the loop exits?
  #3  
Old 14-Sep-2008, 21:06
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Printing a range of inputted values?


Sorry.

I want it to store all the values entered, then print the values that fall into the range >=50 && <=75 instead of each value that is correct printed after each one.

Sorry for not being clear.
  #4  
Old 14-Sep-2008, 21:15
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Printing a range of inputted values?


Quote:
Originally Posted by 8100 Power
I want it to store all the values entered, then print the values that fall into the range >=50 && <=75 instead of each value that is correct printed after each one.
In other words, you are needing to look at what data structures are available for storage.

The simplest solution would be to store results in an array, but the downside of arrays is that they are of finite size. In other words, you would only be able to store values as array elements only up to the what the array can hold.

There is a way in which a (somewhat) unbounded numbers can be stored, but I suspect you haven't discussed dynamic storage through either operator new() or functions like malloc() yet.
  #5  
Old 14-Sep-2008, 21:20
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Printing a range of inputted values?


Quote:
Originally Posted by ocicat
In other words, you are needing to look at what data structures are available for storage.

The simplest solution would be to store results in an array, but the downside of arrays is that they are of finite size. In other words, you would only be able to store values as array elements only up to the what the array can hold.

There is a way in which a (somewhat) unbounded numbers can be stored, but I suspect you haven't discussed dynamic storage through either operator new() or functions like malloc() yet.

I actually haven't. Looks like I'll be printed them one at a time...Thanks for your help!
  #6  
Old 14-Sep-2008, 21:53
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Printing a range of inputted values?


Quote:
Originally Posted by 8100 Power
Looks like I'll be printed them one at a time...Thanks for your help!
Again, you can implement basically what you ask for by using an array, but you will also need to keep a count of what values have been received as input so you don't step past the end of the array.
  #7  
Old 14-Sep-2008, 23:45
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Printing a range of inputted values?


CPP / C++ / C Code:
/* malloc()! are you trying to scare him off?  At least he posted SOMETHING 
    As Ocicat mentioned  an 'array' data structure can be easily 
    impilemented and is safe to use as long as you put certain precautionary 
    measures in place. Keep in mind that an array are stored in a contiguous 
    group of bytes in memory. If you read or write too far you will mess up 
    other data in this program or possibly in another program like your bank 
    accounting program you have running there... 
    Start by learning yer basic 'loop through an array'.
    Look up 'array's in book or online tutorials.
    It's actually easier to start with character arrays, (referred to as 'strings') 
    but working with what you posted, here are some concepts to get you going:
*/
#include <iostream>
using namespace std;

int main ()
{
    const int max = 100;
    int nums[max]= {0};    /* an array of 100 ints initialed to zero */
    int count, loop= 0;

    cout << "Input positive values, input 0 when you wish to stop." << endl;

    /* while ( nums != 0 ){  ... a 'for'loop offers more options , here we
       assign starting value, check the condition and increment the count */
    for( loop = 0; loop < max; loop++ )
    {
        cin >> nums[loop];    /* get the input for nums[0] , nums[1] etc */
        if( nums[loop] == 0 )  /* until user enters a zero                */
        {
          cout << "Ending Input Phase " << endl;
          break;              /* break out of the for loop */
        }
    }   /* for simplicity here we 'break' out of the for loop.  That could be 
           done in the conditional part of the 'for' statement as well.  
    Now move into the printback phase which is pretty much the same loop: */
    count = loop;
    for(loop = 0; loop < count && loop < max; loop++)
    {
      if ( nums[loop] >= 50 && nums[loop] <= 75)
        cout << "The range:" << nums[loop] << endl;
    }
    getchar();    /* don't use  system("PAUSE"); getchar works fine */
    return 0;
} 
/* Now you finish sprucing it up and move directly into malloc() (kidding) 
   one step at a time  */
Last edited by Howard_L : 15-Sep-2008 at 00:18.
  #8  
Old 17-Sep-2008, 14:32
8100 Power 8100 Power is offline
New Member
 
Join Date: Sep 2008
Posts: 24
8100 Power is on a distinguished road

Re: Printing a range of inputted values?


Thanks a million Howard!
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert RGB to luma and Chroma component maximuz C Programming Language 17 12-Feb-2008 13:54
Please help me to convert a simple C++ program into an object-oriented one. zekesteer C++ Forum 5 05-Jan-2008 11:05
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 17:19
Need help generating a random number Allie C Programming Language 7 09-Nov-2005 23:18
paasing values in fk_t_diectories zuzupus MySQL / PHP Forum 6 12-Aug-2003 08:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.