|
Post by fwiss on Dec 17, 2010 21:27:03 GMT -5
Hello, guys. Sorry I always have to be the question-asker and not the helpful answer-giver. Anyhoo, I have a major problem with a 2 dimensional char array. "Too many initializers" I also used an if then statement that seems to think I compared a bool to a char.
//Character to Ascii - Decimal. #include <iostream> #include <string> using namespace std; char userinput; int ascii[8][16]={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127};
char input[8][16];
char input[8][16]={ {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " "}, {" ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ " ", /**/ "l", /**/ "m", /**/ "n", /**/ "o"}, {"p", /**/ "q", /**/ "r", /**/ "s", /**/ "t", /**/ "u", /**/ "v", /**/ "w", /**/ "x", /**/ "y", /**/ "z", /**/ "{", /**/ "|", /**/ "}", /**/ "~", /**/ " "}};
int main () { cout << "Requesting Potato\n"; cin >> userinput;
for(int position=0;input[position]=!userinput; ++position) if(input[position==userinput]) cout << ascii[position];
return 0; }
|
|
|
Post by Supermonkey on Dec 18, 2010 6:35:15 GMT -5
Why are you doing this in the first place? Can't you just do:
int Toint(char c){ return (int)c; }
Also you aren't providing us with a lot of error information so basically I'm going to take a guess and say you've either added too many array elements or you've missed inner braces when declaring ascii. I don't have a compiler on hand to take a closer look so I could be wrong.
|
|
|
Post by fwiss on Dec 18, 2010 9:08:52 GMT -5
Sorry. I believe the errors are something about the if then statement trying to compare bool to char. The harder problem seems to be that the second array (the char array) is never working. 'Too many initializers'. F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp|22|error: too many initializers for 'char [16]'| F:\Program\Computer Arrays.cpp||In function 'int main()' F:\Program\Computer Arrays.cpp|30|error: incompatible types in assignment of 'bool' to 'char [16]'| ||=== Build finished: 9 errors, 0 warnings ===|
|
|
|
Post by Supermonkey on Dec 18, 2010 12:38:27 GMT -5
The bool to char issue is just because you've put the closing square bracket in the wrong place when doing your array lookup:
for(int position=0;input[position]=!userinput; ++position) if(input[position]==userinput) cout << ascii[position];
That should work.
As far as the other issue goes I think it's because you are trying to initialise an 8x16 array but somewhere along the line you are inserting more than 8 or 16 elements. I'd check you've intialised the array with the correct number of elements first.
|
|
|
Post by chris857 on Dec 18, 2010 18:46:33 GMT -5
What is this program supposed to do? If you want to take ascii characters and find their codes and vice versa, here's a much simpler method, pretty much what Supermonkey said:
#include <iostream>
using namespace std;
char charInput; int intInput;
int main () { cin >> charInput; cout << (int)charInput << endl;
cin >> intInput; cout << (char)intInput << endl; system("pause"); return 0; }
As for your original program, an important note is that chars are denoted with single-quotes ['], not double-quotes ["] like you were using. Here is your code in a working form:
//Character to Ascii - Decimal. #include <iostream> using namespace std; char userinput; int ascii[8][16]={{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, {64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, {80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, {96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111}, {112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127}}; int main () { char input[8][16]={{' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' '}, {' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' ', /**/ ' '}, {' ', /**/ '!', /**/ '"', /**/ '#', /**/ '$', /**/ '%', /**/ '&', /**/ '\'', /**/ '(', /**/ ')', /**/ '*', /**/ '+', /**/ ',', /**/ '-', /**/ '.', /**/ '/'}, {'0', /**/ '1', /**/ '2', /**/ '3', /**/ '4', /**/ '5', /**/ '6', /**/ '7', /**/ '8', /**/ '9', /**/ ':', /**/ ';', /**/ '<', /**/ '=', /**/ '>', /**/ '?'}, {'@', /**/ 'A', /**/ 'B', /**/ 'C', /**/ 'D', /**/ 'E', /**/ 'F', /**/ 'G', /**/ 'H', /**/ 'I', /**/ 'J', /**/ 'K', /**/ 'L', /**/ 'M', /**/ 'N', /**/ 'O'}, {'P', /**/ 'Q', /**/ 'R', /**/ 'S', /**/ 'T', /**/ 'U', /**/ 'V', /**/ 'W', /**/ 'X', /**/ 'Y', /**/ 'Z', /**/ '[', /**/ '\\', /**/ ']', /**/ '^', /**/ '_'}, {'`', /**/ 'a', /**/ 'b', /**/ 'c', /**/ 'd', /**/ 'e', /**/ 'f', /**/ 'g', /**/ 'h', /**/ 'i', /**/ 'j', /**/ 'k', /**/ 'l', /**/ 'm', /**/ 'n', /**/ 'o'}, {'p', /**/ 'q', /**/ 'r', /**/ 's', /**/ 't', /**/ 'u', /**/ 'v', /**/ 'w', /**/ 'x', /**/ 'y', /**/ 'z', /**/ '{', /**/ '|', /**/ '}', /**/ '~', /**/ ' '}}; cout << "Requesting Potato\n"; cin >> userinput; for(int x = 0; x < 8; ++x){ for(int y = 0; y < 16; ++y){ if(input[x][y] == userinput) cout << ascii[x][y]; } } system("pause"); return 0; }
EDIT: I didn't get any errors about too many initializers with your code, just:
redefinition of `char input[8][16]' `char input[8][16]' previously declared here In function `int main()': incompatible types in assignment of `bool' to `char[16]'
|
|
|
Post by fwiss on Dec 18, 2010 19:28:25 GMT -5
Oops, yup I redefined it becase I saw something... anyway, it seems to me that the double quotes Must have been the problem. Maybe the brackets too. And I already undefined the first declaration. Thanks for the help, still don't know about that bool to character error.
EDIT: Fixed every error except for |30|error: incompatible types in assignment of 'bool' to 'char [16]'|
Line 30 is this, for reference: for(int position=0;input[position]=!userinput; ++position)
|
|
|
Post by chris857 on Dec 18, 2010 22:22:08 GMT -5
In c++, assignment uses a single '=', but comparison uses a '=='. The compiler sees "input[position]=!userinput" as assiging "!userinput" to "input[position]". The exclamation mark, in this case, means a negation of a boolean and so the compiler sees a boolean being put into a char, hence to the error.
C++'s comparison operators include '==' (equals), '<=' (less than or equal), '>=' (greater than or equal), '!=' (not equal).
|
|
|
Post by Supermonkey on Dec 19, 2010 7:11:18 GMT -5
Ha! I totally missed that, good job!
|
|
|
Post by fwiss on Dec 19, 2010 8:26:10 GMT -5
Oh! I put the ! in the wrong place! Once that is fixed, I get the error: |30|error: ISO C++ forbids comparison between pointer and integer| Line 30 is now this: for(int position=0;input[position]!=userinput; ++position) For some reason, I seem to be comparing position to userinput. How do I fix this? Do I make userinput a 1 by 1 char array and compare input[position] to userinput[1]?
|
|
|
Post by fwiss on Jan 15, 2011 17:33:39 GMT -5
Well, I still can't figure out the problem. Necrobump.
|
|
|
Post by matthew on Jan 15, 2011 23:10:04 GMT -5
Could you post up your complete code as it looks now?
|
|
|
Post by fwiss on Jan 27, 2011 11:04:35 GMT -5
Sorry for being unhelpful on a topic needing help, but the only problem now is a warning when I do this: (Full codes too long to post)
int stats[7]; stats= {1, 15, 15, 10, 10, 3, 2}; I get warning "extended initializer lists only available with -std=c++0x or -std=gnu++0x" on my second posted line.
|
|
|
Post by matthew on Jan 27, 2011 14:27:31 GMT -5
That's easy to fix just change the two lines to this.
int stats[7] = {1, 15, 15, 10, 10, 3, 2};
|
|
|
Post by fwiss on Jan 27, 2011 18:14:56 GMT -5
Thanks! What if I change it after Ive both declared it and assigned a value? Also, thank you for your patience.
Strange, my while function is wonking out. It says: 'while (stats[2]>0)' And seems to skip to outside the function. I want to test if the third element in my array is greater than zero. Did I do something wrong?
Screw it, here's the relevant code segment:
{ chooseclass: cout << "Choose Class: \n1:Rogue \n2:Warrior \n3:Healer\n\n"; char chartype; cin >> chartype; if(chartype=='1') //If Rogue is chosen: int stats[7]= {1, 20, 20, 5, 5, 5, 2}; else if(chartype=='2')//If Warrior is chosen: int stats[7]= {1, 17, 15, 3, 3, 7, 3}; else if(chartype=='3') //If Healer is chosen: int stats[7]= {1, 15, 15, 10, 10, 3, 2}; else goto chooseclass; while (stats[2]>0) {//start main game loop. Encounter://Enemy encounter. cout << "An enemy has appeared.\nWhat will you do?\n1:Attack\n2:Spell\n3:Item\n4:Run"; cin >> enemychoice; }
note that int stats[7]; was declared before this.
|
|
|
Post by DJLinux on Jan 28, 2011 9:17:02 GMT -5
Hello fwiss, so far i know.
The syntax = {x,y,z} is only to initialize an array with predefined values
type name[MAXITEMS] = {values,...}
But you use it more as multiply runtime assignment of values type name = {values,...} ...
looks a litle bit wrong for me.
Is it allowed in C++ ? I don't know.
try to define the array only once
Joshy
|
|