Changing variable's type ... basic

This is a discussion on Changing variable's type ... basic within the C++ Programming forums, part of the General Programming Boards category; Hi, this is "elementary my dear watson" stuff, but it beats me. I use the following: Code: cout<<"enter a file ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8

    Changing variable's type ... basic

    Hi,

    this is "elementary my dear watson" stuff, but it beats me.

    I use the following:

    Code:
    cout<<"enter a file name\n\n";
        cin>>filenam;
        filename = filenam += ".txt";
        ifstream codedfile( filename.c_str() );  
        if ( codedfile.is_open() )
        {
        codedfile>>sentstring;
    to open a file and read it. It's a text file and contains just the string 169abc456fgp
    so now
    the value of the variable sentstring is 169abc456fgp
    - correct?

    The following sort of thing:
    Code:
    int une;
    une = sentstring[0];
    cout<<"first element in sentstring is: "<<une;
    should show that une is 1.

    But that's not an integer 1, it's a character from a string, so I can't expect that the following:

    Code:
    int deux;
    deux= une + 9:
    will yield a value of 10.
    I must first convert une to type int.

    However, if I try it:

    Code:
    int une, deux, trois;
    une = sentstring[0];
    cout<<"first element in sentstring is: "<<une;
    //convert char 1 to int 1
    deux=(int) une;
    trois=deux + 9;
    cout<<"first element in sentstring PLUS 9 = "<<une;
    I get 53 or something equally mad.

    There must be a simple answer.

    In the same vein ...
    in the above example, with sentstring being 169abc456fgp
    how do I get my code to convert 1 and then 6 and then 9
    to integers, but then to realise that the same thing cannot be done with a and b and c, and then to resume converting to integers at 4 and 5 etc etc. ??

    Thanks in anticipation

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    These are ASCII characters. Look at this table: http://www.lookuptables.com/
    You'll see that '1' is 49, and so on. You can just say something along the lines:

    Code:
    int une = sendstring[0] - '0';    // This subtracts the ASCII value of '0' making it "normal"
    int deux = une + 9;               // So on and so forth, do what you wish.
    You should have the normal numbers that you sought in the first place.
    >> how do I get my code to convert 1 and then 6 and then 9 to integers

    You can use the isalpha(..) function to check if it a letter, and if it is not then subtract '0' from it like I showed above.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8
    Thanks, that's very helpful.
    The sites you referred me to are excellent and I have bookmarked them for later.
    However, I still get no joy, because

    Code:
    int une = sentstring[0] - '0';
    cout<<"First element in sentstring is "<<une;
    is only true if sentstring[0] is 0.

    If I try to get smart, as follows:

    Code:
    int middleman;
    middleman = sentstring[0];
    une=middleman - 'middleman';
    which applies your solution regardless of the value of var middleman, I get the error message
    multi-character character constant

    so obviously it is waiting to convert the type of middleman
    to turn it into an ordinary digit.
    But how do you do it?!!?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Can we clarify what you're doing with this?
    Do you want to break 169abc456fgp to like

    int 129
    string abc
    int 456
    string fgp

    ? I'm just a bit confused about your intentions

  5. #5
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8
    I need to do a lot of operations on the string. This was all easy in Javascript where the processing could be done in a browser but I need a stand-alone exe that will do the same thing.

    For a string like
    24839971204732176aBIjwOPfdajdsaklfjasQWREUUIORPEQW #
    tasks include:
    get first element as a digit

    in this example where first element is 2,
    get next 2 elements as a two digit number

    extract the alphabetical part between the numbers and the hatch as a new string

    reverse the order of the elements in this new string

    and many more:

    however, my aim is not just to get this job done, I want to learn the principles.

    In Javascript with its weak typing you can just do something like

    Code:
    x = string[0]  // gets first element as a number  e.g. 2
    substring=substr(string,1,x) // gets next x elements as an x-long number  e.g. 48
    then you'd probably use ereg with regular expressions to identify where the alphabetical part began.
    You might use a break statement like

    Code:
    if (string(n)==#){break} // to identify the end of the alphabetical part
    and so on.
    If Javascript failed to treat a digit as a number you might have to use eval() once in a blue moon.
    So, all my problems seem to be with variable type.

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    You'd probably find the atoi() function the easiest to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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