Jump to content

Recommended Posts

Posted (edited)

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
Posted (edited)

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
Posted

[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?

Posted
i'm trying atoi, it's not working. it replies:

cannot convert parameter 1 from 'char' to 'const char *'

Can you post a snippet of how you're using it?

Posted

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;
}

Posted

After some googling, (and then some self hatred) I found that atoi only works for cstrings, not just a char.

What does your input file look like?

Posted
After some googling, (and then some self hatred) I found that atoi only works for cstrings, not just a char.

What does your input file look like?

the input file looks like, for example:

4445

Posted
the input file looks like, for example:

4445

where 4445 is one number or 4 separate numbers?

by the way with the code I posted you should be able to get it to work. All you have to do is change the char part.

Posted

if the input file is "44459103123" where each digit is its own separate number then the initial code i posted should be all you need for this.

Posted

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;
}

Posted

dont you have to open the file? Try doing it like this:

ifstream input;
input.open("input.txt", ifstream::in);
or just
input.open("input.txt");

and that should work

Posted

It's not generating an output file cause you never call ofstream...

You have an ifstream in, but no ofstream out.

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.

  • Recently Browsing   0 members

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