Accessing other .cpp files

This is a discussion on Accessing other .cpp files within the C++ Programming forums, part of the General Programming Boards category; that part just means that if you hit enter without entering in any values or characters or anything, it will ...

  1. #16
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    that part just means that if you hit enter without entering in any values or characters or anything, it will be invalid input as well. cin >> f1 won't catch you if you just hit the enter key. it just waits for more input. so you need that extra check (cin.get() != '\n') to see if the user just hits enter.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  2. #17
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    while (!(cin >> f1) || cin.get() != '\n')
    
    // while - when cin reads f1 it returns true OR when cin retrieves a character and it's not a newline - loop
    Basically it reads in the float. Then it reads in another character. If it isn't a newline character (meaning it's read in the entire buffer up until the user pressed enter) it will loop. Eliminating this "2.5345asad" from not returning an error. But as 7stud noted, this "423.324 a" will return an error because the extraction operator stops at all whitespace including spaces.
    Sent from my iPadŽ

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    But what is the literal translation of adding this portion of code to the while loop?
    Code:
    || cin.get() != '\n'
    OR, read the next char, and state whether it is not equal to '\n'.

    When a user enters data like this:

    13.5

    and then hits return, an invisible '\n'(which is a newline character) is added to the end of the input. So the input looks like this:

    13.5\n

    The result of Daved's statement is that if the user enters some data like this:

    13.5a\n

    the 13.5 will be read into the double variable successfully, but since the next character is not a '\n', the data will be deemed invalid.

  4. #19
    Registered User motarded's Avatar
    Join Date
    Feb 2006
    Location
    Djibouti, Africa
    Posts
    14
    SM,
    I compile and run the code that Daved posted, and if I enter "4 ajfie" it still reads as invalid input. The only way it will accept the input is if it is a float, and only a float...

  5. #20
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Yes, I know, that's the point. Sorry if I didn't make it clear, but consider if I removed the characters at the end and only put a space:

    "12.625 "

    That is also invalid input because of the space. Sure, you could ask the user why they put the space there, but regardless, at first glance to an end user it looks like a valid float and on a proper program should be accepted. I didn't mean to type the characters after the space to imply bad input, I typed it to imply most data after the space.
    Sent from my iPadŽ

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  2. accessing files over wireless network
    By BobMcGee123 in forum Tech Board
    Replies: 4
    Last Post: 07-29-2006, 03:25 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Replies: 4
    Last Post: 06-11-2004, 07:18 PM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 03:57 PM

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