Quantcast

Maximum PC

It is currently Fri Dec 19, 2014 5:58 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Reading a set of int form standard input in c++?
PostPosted: Fri Feb 04, 2005 6:18 pm 
8086
8086

Joined: Sat Oct 09, 2004 4:40 pm
Posts: 30
I need to write a program that reads a set of integers from standard input and draws a bar chart to standard output using aterisks and spaces.

Code:
For example, if the user entered in the terminal

$ 1 2 3 4 0

the program must print:

      *
    * *
  * * *
* * * *
 


I'm not getting the idea of using cin to get more than 1 integer, can any one help

thanks


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 6:30 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Code:
cin >> obj1 >> obj2 >> obj3;
will assign the objects entered in stdin separated by spaces to obj1, obj2, and obj3.


Alternatively, you can do something like this (where <DELIM> is the delimiting character):
Code:
std::vector<int> ints;
std::string buf;

while(std::getline(std::cin, buf, '<DELIM>'))
{
    ints.push_back(std::atoi(buf.c_str()));
}

//now you have unlimited input of integers (only restrained by how much memory you have all in a vector     

Though I suspect the second example is overkill :)


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 04, 2005 6:46 pm 
7yrs+11,000 Posts
7yrs+11,000 Posts
User avatar

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
you could do it wil batch.


Top
  Profile  
 
 Post subject: Re: Reading a set of int form standard input in c++?
PostPosted: Sat Feb 05, 2005 4:38 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
pascar 802.11 wrote:
I need to write a program that reads a set of integers from standard input and draws a bar chart to standard output using aterisks and spaces.


Do you know how many integers the user will enter before hand?

Also, are they entering them from the command prompt like so...
>myProgram.exe 1 4 3 9 4

This makes things much easier.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Feb 05, 2005 4:38 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
gamerfreak wrote:
you could do it wil batch.

What is that suppose to mean?


Top
  Profile  
 
 Post subject: Re: Reading a set of int form standard input in c++?
PostPosted: Mon Feb 07, 2005 12:29 am 
Coppermine
Coppermine

Joined: Wed Jul 07, 2004 11:07 am
Posts: 666
Location: Woodland Hills, CA
ouch, vertical bars ehh?

well, if this is for a class...then i doubt they (faculty) will let you use the ncurses library for this. This seems like an exercise in forloops?
pascar 802.11 wrote:
I need to write a program that reads a set of integers from standard input and draws a bar chart to standard output using aterisks and spaces.

Code:
For example, if the user entered in the terminal

$ 1 2 3 4 0

the program must print:

      *
    * *
  * * *
* * * *
 


I'm not getting the idea of using cin to get more than 1 integer, can any one help

thanks


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 12:30 am 
Coppermine
Coppermine

Joined: Wed Jul 07, 2004 11:07 am
Posts: 666
Location: Woodland Hills, CA
ya, i couldn't figure it out either.
Gadget wrote:
gamerfreak wrote:
you could do it wil batch.

What is that suppose to mean?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:20 am 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
He means you could write this program with a DOS script. Of course, since this seems like a homework assignment, that probably wouldn't be okay.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:23 am 
Coppermine
Coppermine

Joined: Wed Jul 07, 2004 11:07 am
Posts: 666
Location: Woodland Hills, CA
Kybo_Ren wrote:
He means you could write this program with a DOS script. Of course, since this seems like a homework assignment, that probably wouldn't be okay.


oh.. a batch file. sure.. but that isn't in c++. i was a bit cornfused.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 9:00 pm 
8086
8086

Joined: Sat Oct 09, 2004 4:40 pm
Posts: 30
I would like to give a thanks to everyone for all your help and I have managed to get program working, look

Code:
#include <iostream.h>
using namespace std;

int main()
{
   int input;
   int size;
   int max = 0;
   const int MAX = 100;
   int values[MAX];

   for (size = 0; size < 100; size++)
   {
      cin >> input;
      if (input == 0)
      {
         break;
      }
      else values[size] = input;
   }

   // find the largest element in the array of values
   // values contains the values
   // size tells us how many values we have   
   for(int i = 0; i < size; i ++)
   {
      if(values[i] > max)
      max = values[i];
   }

   // print the chart
   for(int row = max; row > 0; row--)
   {
      for(int col = 0; col < size; col++)
      {
         if(values[col] >= row)
         {
            cout << "*";
         }
         else
         {
            cout << " ";
         }
      }
      cout << endl;
   }

   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 9:10 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Quote:

#include <iostream.h>


Don't use that header; it's deprecated.

Use <iostream>, not <iostream.h>.
If you have a teacher that tells you to use <iostreamh>, politely tell him he is wrong. If he insists upon it, rate him on ratemyprofessor.com


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 11:16 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
The program looks good.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 11:17 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Kybo_Ren wrote:
Quote:

#include <iostream.h>


Don't use that header; it's deprecated.

Use <iostream>, not <iostream.h>.
If you have a teacher that tells you to use <iostreamh>, politely tell him he is wrong. If he insists upon it, rate him on ratemyprofessor.com

LOL... and then forge an email from Bjarne scolding him. :D


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.