Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Super Simple Download Calculator (if/else stuff)

  1. Jan 18, 2014 #1

    TheDemx27

    User Avatar
    Gold Member

    Just upgraded my internet, and I want to upgrade my download calculator along with it, since I find many programs give me shaky and inaccurate download times. I rigged my network so I can switch between my slow speed and fast speed DSL. The problem is the "if / else" statement at the end of main. No matter whether I give it a "f" or a "s" it executes whatever is under the "if"; in this case "FastSpeed();"

    Any responses are appreciated.

    Code (Text):
    #include <iostream>
    #include <string>
    #include <Windows.h>

    using namespace std;

        float MB;
        int Time;
        int Time2;
        int Time3;
        string speed;
       
    int GetInput(){
       
        cout << "Insert Megabytes" << endl;
        cin >> MB;

        return MB;
    }

    static int FastSpeed(){
               
                Time = MB * 1.62;
                Time2 = Time / 60;
                Time3 = Time2 / 60;

                cout << endl << "It will take " << Time2 << " minutes to download this." << " (" << Time3 << "hours)" << endl;
                return 0;
    }

    static int SlowSpeed(){
       
                Time = MB * 10;
                Time2 = Time / 60;
                Time3 = Time2 / 60;

                cout << endl << "It will take " << Time2 << " minutes to download this." << " (" << Time3 << "hours)" << endl;
                return 0;
    }

    int main(){

        system ("title Demx's Download Calculator");
        system ("color a");

        cout << "------------------------------------------" << endl;
        cout << "WELCOME TO DEMX's DOWNLOAD CALCULATOR v1.0" << endl;
        cout << "------------------------------------------" << endl << endl;

        GetInput();

        cout << "FAST or SLOW net? (f/s)";
        cin >> speed;

            if (speed == "f" || "F"){
                FastSpeed();
            }
            else if (speed == "s" || "S"){
                SlowSpeed();
            }
            else {
                cout << "Please enter a valid choice:";
            }
       
            system ("PAUSE");
            return 0;
        }
     
     
  2. jcsd
  3. Jan 18, 2014 #2
    your if statement is always true, when you typed "if (speed == "f" || "F")", you are asking if speed == "f" is true, or the statement "F" is true. "F" is true, so the first line is run.

    the line should read:

    if (speed == "f" || speed == "F"){
    ...
    }
     
  4. Jan 18, 2014 #3

    DrClaude

    User Avatar

    Staff: Mentor

    (speed == "f" || "F") doesn't mean what you think it means. It tests for speed == "f", which can be true of false, then evaluates "F" as true (since it is not zero), then applies the or operator, so the results is always true.

    What you want is if (speed == "f" || speed == "F")
     
  5. Jan 18, 2014 #4

    jedishrfu

    Staff: Mentor

    Try using (speed[0] == 'f') with single quotes around the 'f', double quotes mean strings in C++ code and so you'd be comparing addresses to addresses. The speed[0] grabs the first character of the string speed

    Alternatively you could use (strcmp(speed,"f")==0) as strcmp returns a 0 if true and of course if(0) is false.

    And also you could switch to a number like 0 for slow and 1 for fast.
     
  6. Jan 18, 2014 #5

    Borek

    User Avatar

    Staff: Mentor

    Alternatively, think about using an equivalent to

    Code (Text):
    if (upcase(speed[0]) == 'F')
    {
    }
     
     
  7. Jan 18, 2014 #6

    jtbell

    User Avatar

    Staff: Mentor

    He's using the C++ 'string' data type, not C-style char* "strings". Comparing real honest-to-gosh strings with == and the other relational operators works in C++. You can also compare them to quoted literal strings (const char*).

    However, since he specifically asks the user to enter a single character (f or s), I'd use a simple 'char' (not 'char*'!) instead of a 'string' here.
     
  8. Jan 19, 2014 #7
    This program is in many ways an example how programs should not be written. For starters, why does it even require the user to select fast or slow network? It can just as easily output both values and save the user some typing (and reduce the complexity of the program).
     
  9. Jan 19, 2014 #8

    TheDemx27

    User Avatar
    Gold Member

    Thanks all. While I'm subscribed to a thread, (I don't know if this is against forum rules or not) can anyone do a quick reference to some online sources for beginning programmers for either C++, or C#?

    I've been on msdn and all I can see there is references for people who already know this stuff/experienced the same concepts with other languages, and code academy doesn't have any C.
     
  10. Jan 24, 2014 #9

    TheDemx27

    User Avatar
    Gold Member

    Yeah, good point. I'm at school now and I'll edit this when I get home. Thanks for the input.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ Super Simple Download Calculator (if/else stuff)
Loading...