Problem with Visual C++.NET

This is a discussion on Problem with Visual C++.NET within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> #include <ctime> using namespace std; int main() { int input; while ...

  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 05:13 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> 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, 05: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, 10:28 AM

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