ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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.
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.