vojtex (08-04-2009)
hey im having an annoyin dev c++ issue. here is my extremely basic code, dw bout context:
im trying to get it to output the char but it wont run.Code:#include <iostream> #include <cmath> #include <windows.h> char a[100]; int main(){ using namespace std; cin >> a; if (a == 'si') { cout << a <<" is 'Yes'"<<endl; } else if (a == 'hh'){ cout <<"baf"<<endl; } else if (a == 'hfs'){ cout <<"fgy"<<endl; } else { cout <<"fua"<<endl; } system ("pause"); return 0; }
when my code was like this:
without the value for the char, it ran but only printed out the first letter, because its a char. but when i put it like this:Code:char a;
i get the error:Code:char a[100];
ISO C++ forbids comparison between pointer and integer
please help.
vojtex (08-04-2009)
Hey, i found your problem.
At all the if( a == 'stuff') you are using 'stuff', when you should be suing "stuff" since single quotations are only for single characters. While double quotations are for strings.
ok i did that and it runs but then if statement dont work. wen i type in si it should type:
but instead it does this statement:Code:{ cout <<"The English translation for "<< a <<" is 'Yes'"<<endl; }
whiich is the else statement so idk wat 2 do nowCode:else { cout <<"fua"<<endl; }
sherif, just try using a string. Its easier for this purpose and will cause you less trouble
strings never work 4 me wen i try to use cin >>
its only eva worked once and even copying the working source code does nothing
You cant compare string using the '==' operator, you need to use strcmp which returns a value of 0 when called and include the string header file.
First off, lets get this character thing straight.
This is a character, it holds a single character, it cannot hold a string, nor compare with a string.Code:char a;
This is a character array, it holds a pointer to an array of characters.Code:char a[100]
You must use the ' ' for a character, otherwise it will not work.Code:char a='a';
You must use the " " for a stringCode:char a[10]="asdaasdsa";
Code:#include <string> #include <iostream> #include <cmath> #include <windows.h> char a[100]; int main(){ using namespace std; cin >> a; if (strcmp(a,"si")==0) { cout << a <<" is 'Yes'"<<endl; } else if (strcmp(a,"hh")==0){ cout <<"baf"<<endl; } else if (strcmp(a,"hfs")==0'){ cout <<"fgy"<<endl; } else { cout <<"fua"<<endl; } system ("pause"); return 0; }
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi
ozza (08-07-2009)
Definitive Solution
I compiled this code and it worked as intended... though Im not sure what that intention was. Anyway Read below and you should have your answer.
Ok the code was so messed up it took me a while to finde the problem. When u make an array such as char a[100] it is important to understand the the variable a is not a char but an array type. In order to select a single element of an array and compare it you must do so like this if(a[1] == a)
This is how your code should look:
BTW: Proper indentation helps to make your code more readable and frankly easier to understandCode:#include <iostream> #include <cmath> #include <windows.h> using namespace std; char a[100]; int main() { cin >> a; if (a[1] == 'si') { cout << a <<" is 'Yes'"<<endl; } //any integer can go in between the brackets [] try using an integer var else if (a[1] == 'hh') { cout <<"baf"<<endl; } else if (a[1] == 'hfs') { cout <<"fgy"<<endl; } else { cout <<"fua"<<endl; } system ("pause"); return 0; }
I recommend you start posting your code in a more readable format so that we can help you easier
Last edited by why06; 08-05-2009 at 05:05 PM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
That is incorrect, when you compare a[1] and 'si' it is wrong. First off, a[i] is actually a character not a string, 'si' is not a character. Using ' ' is only for characters, " " for strings. Therefore when you compare a[1] and 'si' you are comparing the second element in the string with the character 's' since 'si' can only hold one character.
There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi
Well yeh ofcourse, 'si' and 'hh' and 'hfs' are not characters therefore the boolean output for all these expressions would be false , but the point was to get it to compile and run, which it did.
If you ask me personally I think its a little foolhardy to try to use characters arrays with out an accurate understanding of character. And honestly the array had no use in this code... its almost complete gobildy goop :L
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
just tested and it works thanks tonnes