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

  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    C++ Calculator
AI Thread Summary
The discussion centers on a user seeking help with a C++ download calculator program after upgrading their internet. The main issue is a logical error in the "if / else" statement where the condition always evaluates as true, causing the program to execute the fast speed function regardless of user input. The correct approach is to explicitly compare the `speed` variable against both "f" and "F" using `if (speed == "f" || speed == "F")`. Suggestions include using single quotes for character comparison, employing `strcmp` for string comparison, or switching to a simpler character type for user input. Additionally, some participants critique the program's design, suggesting it could be simplified by calculating both download times simultaneously instead of requiring user input for speed selection. The thread also includes requests for beginner programming resources in C++ and C#.
TheDemx27
Gold Member
Messages
169
Reaction score
13
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:
#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;
	}
 
Technology news on Phys.org
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"){
...
}
 
(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")
 
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.
 
Alternatively, think about using an equivalent to

Code:
if (upcase(speed[0]) == 'F')
{
}
 
jedishrfu said:
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)

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.
 
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).
 
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.
 
voko said:
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).

Yeah, good point. I'm at school now and I'll edit this when I get home. Thanks for the input.
 
Back
Top