Issues with using CHAR with IF/ELSE conditions.

This is a discussion on Issues with using CHAR with IF/ELSE conditions. within the C++ Programming forums, part of the General Programming Boards category; Hey, sorry if this is already posted somewhere, I did a cursory glance over and saw nothing, so here I ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    Exclamation Issues with using CHAR with IF/ELSE conditions.

    Hey, sorry if this is already posted somewhere, I did a cursory glance over and saw nothing, so here I am.

    I'm writing the second draft of a program I'm trying to make to keep a tally of sales my company's had over the last two months, and I think I'm doing alright so far, but I'm running into a complication with IF/ELSE statements in reference to CHAR inputs.

    It debugs fine, but whenever I go to enter the month(June or July)it runs the "else" condition to terminate the program.

    Here's the code:

    Code:
    /* First Draft Program for June/July Sales */
    
    #include <iostream>
    using namespace std;
    
    
    int julydeliveries = 5463;
    int julycpus = 1212;
    int julyrepairs = 768;
    int julytotals = julydeliveries + julycpus + julyrepairs;
    int junedeliveries = 2352;
    int junecpus = 1256;
    int junerepairs = 247;
    int junetotals = junedeliveries + junecpus + junerepairs;
    
    
    int main()
    {
        char month [5];
        cout << "Totals for: ";
        cin >> month;
        if (month == "june")
        {
             
        cout << "Deliveries: " << junedeliveries << "\n\n";
        system("pause");
        cout << "\n\nCustomer Pick-ups: " << junecpus << "\n\n";
        system("pause");
        cout << "\n\nRepairs: " << junerepairs << "\n\n";
        system("pause");
        cout << "\n\nTotal Sales: " << junetotals;
        cout << "\n\n";
        
    }
        else if (month == "july")
        {
             cout << "Deliveries: " << julydeliveries <<"\n\n";
             system("pause");
             cout << "\n\nCustomer Pick-ups: " << julycpus << "\n\n";
             system("pause");
             cout << "\n\nRepairs: " << julyrepairs << "\n\n";
             system("pause");
             cout <<"\n\nTotal Sales: " << julytotals;
             cout << "\n\n";
    }
        else
        {
            cout << "You must enter either June or July! Terminating Program!\n\n";
            }
            
            
        system("pause");
        
        return 0;
    }

    And, here's the outcome:

    Code:
    Totals for: June
    You must enter either June or July! Terminating Program!
    
    Press any key to continue . . .
    Thanks, in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    If you want to compare such C-style null terminated strings, then you should use std::strcmp() from <cstring>. At the moment, you are comparing pointers, not strings.

    A likely better fix would be to use std::string from <string>, and thus be able to write (month == "june") since std::string has overloaded operator== for string comparison.

    By the way, this code really should go into the main() function:
    Code:
    int julydeliveries = 5463;
    int julycpus = 1212;
    int julyrepairs = 768;
    int julytotals = julydeliveries + julycpus + julyrepairs;
    int junedeliveries = 2352;
    int junecpus = 1256;
    int junerepairs = 247;
    int junetotals = junedeliveries + junecpus + junerepairs;
    You also should try and indent your code more clearly so that things like braces match properly. It is good that you used code tags though
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Wow, okay. You'll have to forgive me, laserlight, I haven't done any coding since my introductory class a year ago. =P

    Uhm, when you say "stdstring::" and so on... What? :P

    I'm clueless as to all that. Sorry!

    If you could be so kind as to tell me where to put the stdstring stuff and how to use it, like what context/where in the program, that'd be great.

    Sorry for any inconvenience!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    You could read the tutorial on C++ strings and also read cppreference.com's entries on C++ strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Ah, thanks a bunch! Those'll help immensely. =)

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Wound up using something from the tutorial you posted, laserlight -- thank you, again. I had found that site earlier, but not found that tutorial.

    Here's what I did:

    Code:
        cout << "Totals for: ";
        string month;
        getline(cin, month, '\n');
        
            if (month == "june")
        {
             
             cout << "\n\nDeliveries: " << junedeliveries << "\n\n";
             
             cout << "\n\nCustomer Pick-ups: " << junecpus << "\n\n";
             
             cout << "\n\nRepairs: " << junerepairs << "\n\n";
             
             cout << "\n\nTotal Sales: " << junetotals;
             cout << "\n\n";
        
        }
        
            else if (month == "july")
        {
             cout << "\n\nDeliveries: " << julydeliveries <<"\n\n";
             
             cout << "\n\nCustomer Pick-ups: " << julycpus << "\n\n";
             
             cout << "\n\nRepairs: " << julyrepairs << "\n\n";
             
             cout <<"\n\nTotal Sales: " << julytotals;
             cout << "\n\n";
        }
    Works perfectly, so far. Now to put in the real data. =P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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