Simple question for a Quick answer

This is a discussion on Simple question for a Quick answer within the C++ Programming forums, part of the General Programming Boards category; So I'm trying to write a program that reads an input file and calculates the sum of numbers. My output ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Simple question for a Quick answer

    So I'm trying to write a program that reads an input file and calculates the sum of numbers.

    My output should look like this:
    inData Sum of numbers
    2301 6
    3302 8
    1234567890 45

    But its turning out like this:

    inData Sum of numbers
    2301 1
    1
    4
    6
    3302 8
    8
    11
    14
    1234567890 14
    23
    31
    38
    44
    49
    53
    56
    58
    59

    So it keeps adding every itineration instead of stopping and going the next input number
    Here is my code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cmath>

    using namespace std;

    int main()
    {
    ofstream fout;
    ifstream indata;
    fout.open(" .out");
    indata.open(" .dat");
    if(!indata)
    {
    fout<<"File does not exist";
    exit(1);
    }
    int number,
    sumDigits=0,
    digit;


    while (indata>>number)
    {
    fout<<number<<setw(6);

    while (number>0)
    {
    digit=number%10;
    sumDigits = sumDigits + digit;
    number=number/10;
    fout<<sumDigits<<endl;
    }}
    return 0;
    }
    Here

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    454
    use code tag and employ better indentation.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Make sure you use code tags whenever you want to preserve indentation. Otherwise multiple spaces will be replaced with one and things won't line up etc. . This makes it difficult to understand:

    inData Sum of numbers
    2301 6
    3302 8
    1234567890 45
    Because what you actually typed was something like:

    Code:
    inData      Sum of numbers
    2301        6
    3302        8
    1234567890  45

    One other thing I noticed - if your input looks like:
    Code:
    2301
    then you can't use cin >> number because then two thousand, three hundred and one will be stored in number instead of 2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a Quick Question
    By Flo in forum C++ Programming
    Replies: 6
    Last Post: 06-25-2010, 05:31 PM
  2. Quick and simple Yes/No question
    By Toonzaka in forum C Programming
    Replies: 9
    Last Post: 04-07-2010, 01:27 PM
  3. Simple question with programming
    By eihn in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 05:09 PM
  4. Replies: 1
    Last Post: 01-23-2002, 02:34 AM
  5. A Simple Question
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 10-26-2001, 05:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21