Results 1 to 12 of 12
  1. #1
    ozza's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    11
    Reputation
    10
    Thanks
    1
    My Mood
    Relaxed

    Question 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.

  2. The Following User Says Thank You to ozza For This Useful Post:

    vojtex (08-04-2009)

  3. #2
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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.

  4. #3
    ozza's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    11
    Reputation
    10
    Thanks
    1
    My Mood
    Relaxed
    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

  5. #4
    zeco's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Canada
    Posts
    683
    Reputation
    12
    Thanks
    78
    My Mood
    Cynical
    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.

  6. #5
    ozza's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    11
    Reputation
    10
    Thanks
    1
    My Mood
    Relaxed
    how 2 fix?

  7. #6
    P1MP's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    In My Broffle With My B1tches
    Posts
    591
    Reputation
    13
    Thanks
    174
    My Mood
    Pensive
    sherif, just try using a string. Its easier for this purpose and will cause you less trouble

  8. #7
    ozza's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    11
    Reputation
    10
    Thanks
    1
    My Mood
    Relaxed
    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

  9. #8
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125
    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;
    }
    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

  10. The Following User Says Thank You to zhaoyun333 For This Useful Post:

    ozza (08-07-2009)

  11. #9
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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
    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

  12. #10
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125
    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

  13. #11
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    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

    "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

  14. #12
    ozza's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    11
    Reputation
    10
    Thanks
    1
    My Mood
    Relaxed
    just tested and it works thanks tonnes

Similar Threads

  1. BIG NOOB here please help learn coding
    By brysonlee in forum C++/C Programming
    Replies: 25
    Last Post: 05-07-2010, 12:28 PM
  2. im new here. please help!
    By maplepawner in forum Combat Arms Hacks & Cheats
    Replies: 1
    Last Post: 03-24-2010, 11:29 PM
  3. [HELP] Please Help WIth Issue
    By allstar185pl in forum Combat Arms Hacks & Cheats
    Replies: 5
    Last Post: 06-09-2009, 02:43 PM
  4. Please Help me here!
    By slimball2001 in forum General Hacking
    Replies: 3
    Last Post: 03-09-2008, 04:40 AM
  5. Help me out here, please...!
    By OutZida in forum Art & Graphic Design
    Replies: 1
    Last Post: 01-12-2006, 11:15 AM

Tags for this Thread