Thread: Problem with Visual C++.NET

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Problem with Visual C++.NET

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include <ctime>
    using namespace std;
    
    int main()
    {
    int input;
      
      while (cout << "Enter a number: " && !(cin >> input))
      {
        cout << "\nInvalid input" <<endl;
        cin.clear();
        cin.ignore(std::numeric_limits < int >::max(), '\n');
      }
      
      cout <<"You entered " <<input;
    return 0;
    }
    This code example works in Dev C++ but not in Visual C++.NET. The error messages I get are:

    Splicer.cpp(41) : error C2039: 'numeric_limits' : is not a member of 'std'
    Splicer.cpp(41) : error C2065: 'numeric_limits' : undeclared identifier
    Splicer.cpp(41) : error C2062: type 'int' unexpected

    In any case I need to be able to trap the error if a user enters an illegal value into an integer variable (such as 'a" or "hahaha") while preventing the program from spiraling into the killer infinite loop.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if you type "123abc" your loop will not detect the "abc" part. The only way I know to do it is to use getline() and get the keyboard input as a string then parse the string for non-numeric characters. you could make a function out of that.

    inlude <limits> to get std::numeric_limits
    Last edited by Ancient Dragon; 04-01-2006 at 06:13 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This code example works in Dev C++ but not in Visual C++.NET.
    You need to #include <limits>, then it will compile and work. There is no need for <cstdlib>, <cstdio>, <ctime>, or <string.h> in that code, though.

    >> if you type "123abc" your loop will not detect the "abc" part. The only way I know to do it is to use getline()
    An easier way is to just change the while control to:
    Code:
    while (cout << "Enter a number: " && (!(cin >> input) || cin.get() != '\n'))
    Basically that also causes the loop to be entered if the next character after the int is read in is not the end of the line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple errors generated by socket headers
    By nocturna_gr in forum Windows Programming
    Replies: 5
    Last Post: 12-16-2007, 06:33 PM
  2. Visual Studio and .Net Runtime Framework dependency
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-08-2007, 07:52 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 03:22 AM
  5. Visual Studio .NET beta 2
    By Marky_Mark in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2001, 11:28 AM