Thread: simple program for char-int

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by ziga View Post
    prog-bman thx for the link i was searching thrue librarys and found a function
    string ( const string& str, size_t pos, size_t n = npos )
    a string isn't a function, its the data type in C++ which is used to represent string data (You might have been using char arrays up til now - but C++ strings are a far better way)


    it works just like i wanted it even displays + or - if i put it in.
    but now if i wanna transform those digits in to numbers using atoi function i cant??
    why wont it work.
    or is there any other way to transform a std::string to integer

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    
    string s5;
    int a;
    cout <<" iput string  ";
    cin>> s5;
      string s3 (s5, 8, 3);
    
    a = atoi (s3);
      cout << "ns3: " << s3<<endl;
    cout << " a =  " << a <<endl;
      return 0;
    }
    Don't use atoi - its unsafe, you can't reliably check for a valid conversion, which can lead to all sorts of problems.

    C++ has a facility called stringstreams which will do what you're after safely
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        std::string str;
        std::cout << "Enter a number: ";
        std::getline(std::cin, str);
    
        std::stringstream ss;
        ss << str << 83;
    
        int i;
        if ( ss >> i )
            std::cout << "Valid conversion to int: " << i;
        else
            std::cout << "Invalid input";
    }
    Last edited by Bench82; 08-21-2007 at 05:50 AM.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    ok i found a way to first transform a std:: string to a c string (char)
    and from char using atoi function to get integer.
    here is te
    Code:
    // strings and c-strings
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <stdlib.h>
    #include <stdio.h>
    using namespace std;
    
    int main ()
    {
      char * cstr, *p;
    
      string str;
    cout << "input a string  ";
    cin >> str;
     string s4( str , 2, 4);
     cstr = new char [s4.size()+1];
    
    strcpy (cstr, s4.c_str());
    int a;
    cout << " cstr =  " << cstr <<endl;
    
    a = atoi (cstr);
    int b;
    b = 100;
    cout  << " a =  " <<a <<endl;
    cout << " b =  " << b + a <<endl;
      return 0;
    }
    is there a more simple way to do this? does this slow down a computer?

  3. #18
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    ok i didnt see youre post bench82 i just posted what ive been doing this morning ill try your program.
    I have a question what is the diference between std::cout and only cout"...?
    were can i find more data on std::stringstream
    http://www.cplusplus.com/reference/i.../stringstream/
    i gues this is not it?
    Last edited by ziga; 08-21-2007 at 06:09 AM.

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by ziga View Post
    ok i didnt see youre post bench82 i just posted what ive been doing this morning ill try your program.
    I have a question what is the diference between std::cout and only cout"...?
    the std:: is a namespace qualifier - in short, its another way of accessing names in the C++ standard library without typing using namespace std; at the top of your program

    if you've put using namespace std; in your code, then you may omit the std:: prefix, I've just included the std:: prefix for the sake of showing that these are standard library names.

    were can i find more data on std::stringstream
    http://www.cplusplus.com/reference/i.../stringstream/
    i gues this is not it?
    Yes, that's one possible reference source. In general, you can use a stringstream in much the same way that you would use cin/cout or fstreams (Except that a stringstream is for string manipulation, rather than I/O )

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if i input a string:
    >> 2345687 h 15432
    >> i am not able to get numbers a = 2345 and b = 5432

    In your original example there were separators between the numbers. If you want to read in 2345687 as 2345, then you have to do special processing, because 2345687 is 2345687.

    Are you saying that the numbers are always at most 4 digits? You have to have rules that define what the numbers will be, or else you cannot write code to enforce the rules.

    If what you want is to read in at most 4 digits into your number, then you should read in 4 digits into a string, then convert that string to an int. You still only need a single int per number.

    As far as atoi or stringstream, I prefer stringstreams, too, but either will work and you should be focusing on the other parts of the program.

    Also, this code:
    Code:
    cstr = new char [s4.size()+1];
    strcpy (cstr, s4.c_str());
    a = atoi (cstr);
    should be changed to this:
    Code:
    a = atoi (s4.c_str());
    Last edited by Daved; 08-21-2007 at 09:49 AM.

  6. #21
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    thx for the info but now i have to read more about the stringstreams, before i can continue with my programming.

  7. #22

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM