if state ment and char arrays

This is a discussion on if state ment and char arrays within the C++ Programming forums, part of the General Programming Boards category; I dont know why this wont work. Anyone got any ideas? Its just a sample of it. Code: #include <iostream> ...

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    if state ment and char arrays

    I dont know why this wont work.
    Anyone got any ideas?
    Its just a sample of it.

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    
        char buffer [20];
        char pick;
        do{
            cin.getline (buffer, 20);
            cout << buffer;
            if (buffer == "add"){
                     cout << "Hey hows it going?";
            }
            if (buffer == "subtract"){
                    cout << "Hey hows it going?";
            }
            if (buffer == "multiply"){
                     cout << "Hey hows it going?";
            }
            
            if (buffer == "divide"){      
                     cout << "Hey hows it going?";
            }
        cin >> pick;
        }while (pick != 'e');
        
    return 0;
    }
    Learning C++
    Programmer in training

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you can't compare strings. replace with
    Code:
    #include <string.h> /*I don't know if it needs the .h(I am a C programmer)*/
    /*code*/
    if(!strcmp("add",buffer){
    /*rest of code*/
    }
    strcmp is different because it returns 0 when strings are equal(note the !) and where is the pick var come into play?

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Yah now im getting a parse error.
    The pick var comes into play so If i ypue e there itll exit.
    remember this is jsut a test prog.
    Learning C++
    Programmer in training

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you need something like this then
    Code:
    (can't code c++ so I'll use pseudo)
    print enter stuff e to exit
    get input
    while(!strcmp(input,"e")){
         if !strcmp(input,blah)
         /*rest of code*/
         /*right before the end of the while loop have*/
         get input
    you don't need a do-while loop for this
    if you do you could do it this way
    Code:
    do{
    /*stuff*/
    while(!strcmp(get(input),"e");

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main ()
    {
    
        char buffer [20];
        char pick;
        do {
            cin.getline (buffer, 20);
            cout << buffer;
            if(!strcmp(buffer,"add")){
                     cout << "Hey hows it going?";
            }
            // the rest of the code
            
        cin >> pick;
        } while (pick != 'e');
        
        return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. invalid conversion from (void *) to unsigned char
    By k0k33 in forum C Programming
    Replies: 1
    Last Post: 02-19-2009, 07:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21