LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2004, 03:58 PM   #1
Fulkan
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Rep: Reputation: 0
little help about my program in c++


Hi@all

I just started to learn C++. I am in trouble with reading integers. I am suppose to write a little program but I have a problem.

In my program, I should read 10 integer values from keyboard and hold them in an array. My program has to halt input either eof or array is full. After that echo each array.

example;

input 1 2 3 4 5 6 7
output 1 2 3 4 5 6 7

My problem is when I use cin for reading values, it skips white space. My loop never ends until array is full. I mean when I want to read and hold 8 numbers, it doesn't work. Can someone show me how to I can write the program?

My code;

Code:
#include <iostream>
using namespace std;
int main()
{
const int size=10;
int array[size];
int input; 
int actual=0; // counter how many numbers are read until eof
input=cin.get();
while (input!=cin.eof()&&actual<size)
{
array[actual]=input;
actual++;
input=cin.get();
}
for (int i=0; i<actual; i++)
cout <<a[i];
return 0;
}
thanks

Last edited by Fulkan; 12-10-2004 at 04:24 PM.
 
Old 12-10-2004, 04:04 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Can someone show me how to I can write the program?
Without the code you have problems with, we'll have to guess where the problem is...

Asking to do your homework is not fair, doesn;t really help you learn, and it's against this forum's rules. If you get stuck, and post the code you have so far, we can help of course.

Last edited by Hko; 12-10-2004 at 04:07 PM.
 
Old 12-10-2004, 04:13 PM   #3
Fulkan
LQ Newbie
 
Registered: Sep 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Hi@ Hko

I am sorry. I forgot to add my code. I am adding my code in my first message now. Actually, my program that I asked for help is the part of my homework but I am not asking whole program. I am asking where I am stuck on it. I think it is fair. I am trying to learn.

When I wrote this code for char, it works. I wonder why it doesn't work for integers.

thanks

Last edited by Fulkan; 12-10-2004 at 04:34 PM.
 
Old 12-10-2004, 06:46 PM   #4
gaffel
LQ Newbie
 
Registered: Mar 2004
Distribution: debian sarge, 2.6.6, and ALSA finally works!!
Posts: 18

Rep: Reputation: 0
it doesn't work for integers because it work for chars.

use some debug options and look at what value cin.get() actually returns into variable input.
Then think about what a char actually is.
 
Old 12-11-2004, 06:23 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Here's one solution.
It reads until ten integers has been read or until a reading
error occurs. One might consider a solution where one tries to
recover from a reading error and continue to read but that is left
as an exercise for the reader.

Code:
#include <iostream>
#include <limits>

using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::flush;
using std::numeric_limits;
using std::streamsize;

int main()
{
   const unsigned short array_size = 10;
   int integer_array[array_size];
   unsigned short current_index = 0;
   int n = 0;

   /* Reads until ten integers has been read or a *
    * reading error occurs. Anything that's left  *
    * in the stream after that is discarded.      */
   while(cin >> n && current_index < array_size)
   {
      integer_array[current_index++] = n;
   }

   if(!cin.good())
   {
      cerr << "cin is in a bad state." << endl;
      cerr << "That means input not automatically "
           << "convertible to int was encountered "
           << "in the input stream." << endl
           << "Clearing error bit so we can discard any "
           << "input left in the stream." << endl;

      /* All operations on the stream, except clear(), are no-ops *
       * until the fail/bad-bit has been cleared.                 */
      cin.clear();
   }

   /* Discarding remaining input in the stream, if any. */
   cin.ignore(numeric_limits<streamsize>::max());

   cout << "Read " << current_index << " integers." << endl;

   for(int i = 0; i < current_index; ++i)
   {
      cout << integer_array[i] << " ";
   }

   cout << flush;

   return 0;
}
 
Old 12-12-2004, 08:25 AM   #6
obashir
LQ Newbie
 
Registered: Nov 2002
Location: Loughborough, UK
Distribution: Redhat 9, Fedora, Yellow Dog
Posts: 29

Rep: Reputation: 15
With reference to the original program, cin.get() returns the ascii value of a char therefore it works for characters.

More info at

http://www.cplusplus.com/ref/iostream/istream/get.html

Regards,

OMAR.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sms program and database in linux web program in windows.. does not see each other.. keikun_naruchan Programming 0 07-06-2005 01:40 AM
Total recovery: Which program? ghost4linux, YaST2? Best drive imaging program? lagu2653 Linux - Software 1 06-20-2005 01:44 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration