Thread: simple program for char-int

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    simple program for char-int

    Hello i am having problems transforming program from char - int

    Code:
    // program that transforms charicters in to integer
    #include <iostream.h>
    #include <cstring.h>
    
    using namespace std;
    
    int main() {
    
    char string[256];
    int a[3];
    int b[3];
    
    cout << "input  - x345 j5678 k345 ";
    
    //i know that my input lines will be always the same,
    //only the numbers will be cahnging.
    
    cin.getline << string;
    
    cout << "your line:  " << string << endl;
    
    //i know thet in filds 1,2,3 of string there are numbers
    //how can i make this numbers to go to integer a so that i culd
    //use mathematical operations on tham?
    
    cout <<" &string[1] " << &string[1] <<endl;
    
    //this inputs the pointer to string[1] and it displays lines from 1 - last
    
    a = &string[1];
    
    cout <<" a =  " << a <<endl;
    
    //i used this  function couse a can only have 3 numbers
    // so in theory it shuld haven numbers 345
    //instead it writes 54 or something
    
    b = string[1];
    
    //if i use this line it displays an error
    //you can not transform char in to int
    
    cin.get();
    //how can i fiks the program, so that i culd read numbers as integers?
    }

  2. #2
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    sorry
    Could not make any sense of it.
    Just tell me what you want to do?

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    I wanna make my program read numbers, from input line.

    but the problem is i also have letters in my input line so i have to use char function.

    but i cannot operate + - * / functions with char i have to transform tham in to int.

    if i try to transform int = char i get an erro when i try to compile it.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have some errors just getting the input from the user. I'd suggest removing all the code inside main (back it up somewhere first). Then start again, but only do a little bit at a time. Start by getting the input from the user and outputting it back. Compile the code and run it. Once it is working move on to the next part.

    Once you are ready, you need to convert a string to an int. One way to do this is using the atoi function.

    You should use the standard C++ header <iostream>, not <iostream.h>. Ideally, in C++ you would use C++ strings, not character arrays like you're doing. C++ strings are in <string>, C string functions are in <cstring> or <string.h>, not <cstring.h>. If you can use C++ strings, then do it, and you can convert your string to an int with a stringstream. If this is an assignment and you aren't allowed to do that then ignore that advice until you have time to study the language on your own.

  5. #5
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    well ziga, I hope this helps you:
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
            string line;
            getline(cin, line, '\n');
            int a, b, c;
            sscanf(line.c_str(), "&#37;d%*c%d%*c%d", &a, &b, &c);
            cout << (a+b+c) << endl;
            return 0;
    }
    
    /*
    input: 1, 2, 3
    output: 6
    */

  6. #6
    Registered User nempo's Avatar
    Join Date
    Aug 2007
    Posts
    39
    I'm rather new to c++ myself but I've found this to work pretty good.

    Code:
    'int' = (int)'char' - 48;
    The stuff within the ' ' (withouth the ' ' signs) should obviously be replaced with corresponding variables.
    'char' can also be a c++ string.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    'int' = (int)'char' - 48;
    -48 assumes ascii encoding tho. -'0' would be more portable. Also I'm pretty sure you can drop the bracketed int conversion part as a character is basically the low 8 bits of an integer in the first place:
    Code:
    some_integer = some_char -'0';

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    ok i try function atoi, it works finehere is the code
    Code:
    // program that transforms charicters in to integer
    #include <iostream>
    #include <stdio>
    #include <stdlib>
    
    using namespace std;
    
    int main() {
    char string[256];
    int a;
    
    cout << "input  - x345 j5678 k345 ";
    cin.getline ( string , 256 , 'k'); //k is my end sign
    
    cout << "your line:  " << string << endl;
    
    a = atoi (string);
    cout <<" a =  " << a <<endl;
    cin.get();
    }
    but why will i get an error if i try to declare
    Code:
    int a[4]; //it means that a can have only 4 numbers
    ...
    a = atoi (string [3]); //here i wanna read 4 numbers from the third field on
    ...
    Code:
    string.cpp: In function âint main()â:
    string.cpp:23: error: invalid conversion from âcharâ to âconst char*â
    string.cpp:23: error:   initializing argument 1 of âint atoi(const char*)â
    string.cpp:23: error: incompatible types in assignment of âintâ to âint [4]â
    ziga@debian:~$

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is a difference between a digit and a number. 345 is one number - three hundred fourty five. It has three digits. Do you want to store digits separately as in 3, 4 and 5?

    If you want to store digits, then int a[4] is appropriate and you should not use atoi to convert. Instead, use string[3] - '0' which is how you convert a single character digit to a number. You have to so this once for each digit.

    If you want to store the entire number, just use int a; so that you are only holdinng the one number as you did in that code.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    Thumbs up

    I wanna hold entire number.
    lets say my string is:
    i 2345 l6543 n1234 k
    i wanna have
    a = 2345
    b = 6543
    c = 1234
    so i can use + - *... with tham.
    so if i read corectly i have to declare that ich integer is 4 digits long and it starts at difrent fields of string

    int a[4] and it starts at string[1]
    int b[4] and it starts at string[6]
    ...

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No. A single int holds an entire number. So int a; can hold 2345. You don't need an array for the ints.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    yes a single int holds 2345.
    but if i have 234587 in string, and i only wanna have the first 4 digits as a number ( a = 2345) it wont work couse it wil input all the digits => a = 234587.
    or if i input a string:
    2345687 h 15432
    i am not able to get numbers a = 2345 and b = 5432
    couse the program will take the first digits until it hits a blank spot or a letter or something other than digit

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

  14. #14
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    printf("%d", (int)character);

    ???


  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    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 )
    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;
    }
    Last edited by ziga; 08-21-2007 at 05:30 AM.

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