Jump to content

C++ Convert Char To Int?


supereater14
 Share

Recommended Posts

i am looking to convert a char array to an int array

example:

"4" becomes 4

never mind, instead, tell me what's wrong with this code:

#include <fstream>

#include <iostream>

using namespace std;

int main(){

int x[25];

int y = 0;

char m[100];

int z[100];

ifstream in("input.txt");

/*if (!in){

cout << "read error\n";

return 1;

}*/

in >> m;

/*for(int b = 0; b == 99; b++){

z[c] = m[c];

}*/

//*z << m;

for (int c = 0; c == 99; c++){

switch(m[c]){

case '1':

y--;

break;

case '2':

y++;

break;

case '3':

x[y]--;

break;

case '4':

x[y]++;

break;

case '5':

ofstream out("output.txt");

out << x[y];

out.close();

in.close();

return 0;

break;

}

}

in.close();

return 0;

}

Edited by supereater14
Link to comment
Share on other sites

You could use atoi which converts strings into an int. If you would like to write your own function for that then something like this would work:

char array[50];
int n[50];
...
for( int i = 0; i &lt; 50; i++ )
    n[i] = (int)array[i] - 48;

That would just convert to single digits. But you get the idea.

EDIT:

instead of that whole switch statement and stuff you can do what I posted. Although depends on how your input file is setup. Assuming in your input it says something like:

1 2 3 4 5 6 7 8

you would have to include something like:

if( array[i] != ' ' )
...

Edited by exxon
Link to comment
Share on other sites

[quote

EDIT:

instead of that whole switch statement and stuff you can do what I posted. Although depends on how your input file is setup. Assuming in your input it says something like:

1 2 3 4 5 6 7 8

you would have to include something like:

if( array[i] != ' ' )
...

wouldn't

wouldn't that stop at the first whitespace?

Link to comment
Share on other sites

The thing about atoi is that its a C function so you would have to include a C library. Cant remember which.

anyways just did this really fast so sorry if its messy and not that optimal

#include &lt;iostream&gt;
#include &lt;cstring&gt;

using namespace std;

int main(){
    char array[] = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15";
    int n[15];
    int x = 0;
    int j = 0;
    for( int i = 0; i&lt;=strlen(array); i++ )
    {
        if( array[i] == ' ' || array[i] == '\0')
        {
            n[j] = x;
            j++;
            x=0;
        }
        else
        {
            x *= 10;
            x += (int)array[i]-48;
        }
    }

    for( int i = 0; i &lt; 15; i++ )
    {
        cout &lt;&lt; n[i] &lt;&lt; endl;
    }
    return 0;
}

Link to comment
Share on other sites

here's the code, but when i run it, it doesn't produce an output file.

#include &lt;cstring&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;
using namespace std;
int main(){
    int x[25];
    int y = 0;
    char m[100];
    int z[100];
    //*char h[100];
    ifstream in("input.txt");
    /*if (!in){
        cout &lt;&lt; "read error\n";
        return 1;
    }*/
    in &gt;&gt; m;
    /*for(int clap = 0; clap == 99; clap++){
        h[clap] = m[clap];
    }*/
    for(int b = 0; b == 99; b++){
    z[b] = (int(m[b]) - 48);
    }
    //*z &lt;&lt; m;
    for (int c = 0; c == 99; c++){
        switch(z[c]){
            case 0:
                break;
            case 1:
                y--;
                break;
            case 2:
                y++;
                break;
            case 3:
                x[y]--;
                break;
            case 4:
                x[y]++;
                break;
            case 5:
                ofstream out("output.txt");
                out &lt;&lt; x[y];
                out.close();
                in.close();
                return 0;
                break;
        }
    }
    in.close();
    return 0;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...