QuestionPLEASE HELP STUPID ISSUE HERE

Posts 1–12 of 12 · Page 1 of 1
PLEASE HELP STUPID ISSUE HERE
hey im having an annoyin dev c++ issue. here is my extremely basic code, dw bout context:


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;
}
im trying to get it to output the char but it wont run.
when my code was like this:

Code:
char a;
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[100];
i get the error:
ISO C++ forbids comparison between pointer and integer
please help.
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:
Code:
{
cout <<"The English translation for "<< a <<" is 'Yes'"<<endl;
}
but instead it does this statement:
Code:
else
{
cout <<"fua"<<endl;
}
whiich is the else statement so idk wat 2 do now
Quote Originally Posted by ozza View Post
ok i did that and it runs but then if statement dont work. wen i type in si it should type:
Code:
{
cout <<"The English translation for "<< a <<" is 'Yes'"<<endl;
}
but instead it does this statement:
Code:
else
{
cout <<"fua"<<endl;
}
whiich is the else statement so idk wat 2 do now
Yeah, i realize that. Well i'm still learning but i think it has something to do with using character arrays, and the if statement.
how 2 fix?
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.

Code:
char a;
This is a character, it holds a single character, it cannot hold a string, nor compare with a string.

Code:
 char a[100]
This is a character array, it holds a pointer to an array of characters.

Code:
char a='a';
You must use the ' ' for a character, otherwise it will not work.
Code:
char a[10]="asdaasdsa";
You must use the " " for a string

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;
}
just tested and it works thanks tonnes
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.

Quote Originally Posted by ozza View Post
Code:
#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;
}
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:
Code:
#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;
}
BTW: Proper indentation helps to make your code more readable and frankly easier to understand
I recommend you start posting your code in a more readable format so that we can help you easier
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.
Quote Originally Posted by zhaoyun333 View Post
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.
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
Posts 1–12 of 12 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us