supereater14 Posted March 30, 2010 Share Posted March 30, 2010 (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 March 30, 2010 by supereater14 Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 (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 < 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 March 30, 2010 by exxon Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 that would actually work Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 [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? Quote Link to comment Share on other sites More sharing options...
requiem Posted March 30, 2010 Share Posted March 30, 2010 Put his snippet in a loop. while (fin) { if ( array[i] != ' ' ) .... } Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 i'm trying atoi, it's not working. it replies: cannot convert parameter 1 from 'char' to 'const char *' Quote Link to comment Share on other sites More sharing options...
requiem Posted March 30, 2010 Share Posted March 30, 2010 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? Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 char blahblah[100]; int hi[100]; ... for (int b = 0; b == 99; b++){ hi = atoi(blahblah; } Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 if it helps, i'm using microsoft's visual c++ 2008 (really annoying compiler) Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 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 <iostream> #include <cstring> 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<=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 < 15; i++ ) { cout << n[i] << endl; } return 0; } Quote Link to comment Share on other sites More sharing options...
requiem Posted March 30, 2010 Share Posted March 30, 2010 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? Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 the entire error is error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' though, meaning that it recognizes atoi Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 (edited) 4 numbers btw, atoi requires a const char*, not a cstring Edited March 30, 2010 by supereater14 Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 well, atoi(&whatever) works (where whatever is a char), but the code still doesn't work as a whole. Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 i think the best solution is (int(whatever) - 48) Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
requiem Posted March 30, 2010 Share Posted March 30, 2010 4 numbers btw, atoi requires a const char*, not a cstring Well all a cstring is is a character array. :P Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 here's the code, but when i run it, it doesn't produce an output file. #include <cstring> #include <fstream> #include <iostream> 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 << "read error\n"; return 1; }*/ in >> 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 << 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 << x[y]; out.close(); in.close(); return 0; break; } } in.close(); return 0; } Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 all right, apparently it cannot find the "input.txt" file Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
requiem Posted March 30, 2010 Share Posted March 30, 2010 It's not generating an output file cause you never call ofstream... You have an ifstream in, but no ofstream out. Quote Link to comment Share on other sites More sharing options...
supereater14 Posted March 30, 2010 Author Share Posted March 30, 2010 It's not generating an output file cause you never call ofstream... You have an ifstream in, but no ofstream out. sure i do, it's under case 5. Quote Link to comment Share on other sites More sharing options...
exxon Posted March 30, 2010 Share Posted March 30, 2010 I still believe you might need that file.open("filename") part. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.