Thread: Reading string from outside data file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Try this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      double input;
      cin >> input;
      cout << input;
    
      double input2;
      cin >> input2;
      cout << input2;
      return 0;
    }
    Try typing in any string for the first input. This program will fail the same way on the next input as in your program. This is because when you attempt to read in a string ("Willy") as a double (some numerical value), cin fails.

    Code:
    in >> fname >> lname >> score;
          string  string  double   <- Expected
          Toots   Sweet     87   <- First iteration
          76       90       -1   <- Second iteration
    I'll leave it up to your creative intellect to figure out how to avoid reading in a string as a double in your case.
    Last edited by jverkoey; 10-29-2007 at 10:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  3. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM