6 Replies - 536 Views - Last Post: 08 September 2012 - 08:17 PM Rate Topic: -----

#1 emk0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 12

Beginner program with integers

Posted 08 September 2012 - 11:00 AM

Hi,
As a complete beginner I am truing to solve some examples that I stumble upon in books and tutorials, this one is givin me headaches for several days:

This is my code:
#include <iostream>
using namespace std; // 
int main()


  int a,b,c,d,e;
  float flyttal;
  cout << "write whole number:";
  cin >> a;
  cout << "you wrote:"<< a << endl;
  cout << "write in five whole numbers:";
  cin >> a >> b >> c >> d >> e;
  cout <<"you wrote:"<< a "\t" << b "\t" << c "\t" << d "\t" << e "\t" << endl;
  int a1;
  cout << "write in a whole number and decimal number:";
  cin >> a >> flyttal;
  cout << "whole number is:" << a << "\n"; // 
  cout << "decimal is :" << flyttal << endl;

  return 0;
}


For this assignment there are 2 RUN requirements

Runexample1- works fine with no problems
Write whole number: 10
You wrote: 10
Write in 5 whole numbers: 12 30 27 13 11
You wrote: 12 30 27 13 11
Write in a whole number and a decimal: 67 3.141592
You wrote whole number: 67
You wrote decimal: 3.1416 (how do I round this number)

Runexample2 - after I type in the decimal number in the whole number field it works fine up until "you wrote 10" and then the program executes the rest of the line without prompting for "cin".

Write whole number: 10.2
You wrote: 10
Write in 5 whole numbers: -12 10330 27 13 11 five
You wrote: -12 10330 27 13 11
Write in a whole number and a decimal: 3267 3.141592 d
You wrote whole number: 3267
You wrote decimal: 3.1416

This is the code that I get after i type in 10.2 .. the program continues without stopping for "cin"
Write whole number:10.2
You wrote:10
Write in 5 whole numbers:You wrote:0-716758921199580386122937002293512
Write in a whole number and a decimal: You wrote whole number::2293456
You wrote decimal:5.88796e-039




Thanks in advance for any assistance

Is This A Good Question/Topic? 0
  • +

Replies To: Beginner program with integers

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 269
  • View blog
  • Posts: 3,839
  • Joined: 08-January 12

Re: Beginner program with integers

Posted 08 September 2012 - 11:07 AM

First of all, where is the starting bracket after int main()? That should give you an error. And as for the problem you have, you are trying to take float input into decimal, that's why it's acting up. You have 3 options:

1. Don't give weird input, which is probably the best for now

2. If you want to give such input, first check that the input is of proper type, and then continue execution. You can do this by taking input first in string, and then converting it to int by the use of some function like atoi()

3. After taking input, check if there were any errors. If not, then all is well. Otherwise, you can either quit or clear the error flags, and ask the user to input the number again.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1939
  • View blog
  • Posts: 5,774
  • Joined: 05-May 12

Re: Beginner program with integers

Posted 08 September 2012 - 11:11 AM

You were prompted for an integer at line 9, and you typed in "10.2", the stream class in the runtime library parses digits that satisfy its definition of an integer, so it picks up the "10", and leaves behind the ".2" on input buffer. When you get to line 12, the first part of that input operation again tries to get an integer. Unfortunately the ".2" leftover in the input buffer puts the stream into an error state, and so the operation fails. And the stream continues to be in an error state through the rest of the program. Since your other variables are uninitialized, you get random numbers displayed.
Was This Post Helpful? 1
  • +
  • -

#4 emk0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 12

Re: Beginner program with integers

Posted 08 September 2012 - 11:15 AM

Sorry that there are mistakes in the code, i was typing it in a hurry, i could not copy the actual code that I have because some of the text is in another language, but the code structure is the same, except the missing brackets after main() and the integer at line 14 that is completely unnecessary.

@Skydiver i also noticed this, that the buffer uses the "leftovers" and continues, is there any way to clear/flush the buffer before continuing?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1939
  • View blog
  • Posts: 5,774
  • Joined: 05-May 12

Re: Beginner program with integers

Posted 08 September 2012 - 11:23 AM

Try using basic_istream::ignore(). So in this case cin.ignore().
http://en.cppreferen..._istream/ignore
Was This Post Helpful? 1
  • +
  • -

#6 emk0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-September 12

Re: Beginner program with integers

Posted 08 September 2012 - 11:33 AM

thanks, i will try it
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,401
  • Joined: 19-February 09

Re: Beginner program with integers

Posted 08 September 2012 - 08:17 PM

View Postemk0, on 08 September 2012 - 09:00 PM, said:

You wrote decimal: 3.1416 (how do I round this number)


Do you mean display a certain number of decimal places such as 3.14?

If so the i/o manipulators (<iomanip>) setprecision and fixed, may help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1