Problems with code from the FAQ

This is a discussion on Problems with code from the FAQ within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble with the code from the Convert a string to a int (C++) I've only changed it ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    1

    Problems with code from the FAQ

    I'm having some trouble with the code from the Convert a string to a int (C++)
    I've only changed it a little bit. Here's what I got so far:

    Code:
    #include <sstream> 
    #include <string>  
    #include <iostream>  //I added this
    using namespace std;  
    
    bool StringToInt(const string &s, int &i);
    
    int main()
    {
      string s1 = "12";
      string s2 = "ZZZ";
      int result;
      
      if (StringToInt(s1, result))
      {
        cout << "The string value is " << s1
             << " and the int value is " << result << endl;
      }
      else
      {
        cout << "Number conversion failed" <<endl;
      }
      if (StringToInt(s2, result))
      {
        cout << "The string value is " << s2
             << " and the int value is " << result << endl;
      }
      else
      {
        cout << "Number conversion failed on " <<s2 <<endl;
      }    
      return 0;
    }
    
    bool StringToInt(const string &s, int &i)
    {
      istringstream myStream(s);
      
      if (myStream>>i)
        return true;
      else
        return false;
    }
    
    
    /*
     * Program output:
     The string value is 12 and the int value is 12
     Number conversion failed on ZZZ
     *
     */
    I get the following error:

    --------------------Configuration: onlyatest - Win32 Debug--------------------
    Compiling...
    thisisatest.cpp
    Linking...
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/onlyatest.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    onlyatest.exe - 2 error(s), 0 warning(s)


    I think the error comes from the fact that "result" is never assigned a value. Can anyone give me a hand on who to fix this? I am using Visual C++ 6.0.

    Thanks

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Build a console application, not a windows application - either create a new project or change subsystem:windows to subsystem:console in the project settings.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 01:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

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