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

In summary, the program asks the user to enter a speed value between "f" and "F". If the user enters "f", the program will run the "FastSpeed()" function. If the user enters "F", the program will run the "SlowSpeed()" function.
  • #1
TheDemx27
Gold Member
169
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
  • #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"){
...
}
 
  • #3
(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")
 
  • #4
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.
 
  • #5
Alternatively, think about using an equivalent to

Code:
if (upcase(speed[0]) == 'F')
{
}
 
  • #6
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.
 
  • #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).
 
  • #8
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.
 
  • #9
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.
 

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

1. What is the purpose of the C++ Super Simple Download Calculator?

The C++ Super Simple Download Calculator is a program designed to calculate the download time and speed of a file based on the user's input of the file size and internet speed.

2. How do I use the C++ Super Simple Download Calculator?

To use the C++ Super Simple Download Calculator, simply enter the file size and internet speed in the designated inputs and press the "Calculate" button. The program will then display the estimated download time and speed.

3. Can I change the units of measurement for file size and internet speed?

Yes, you can change the units of measurement for file size and internet speed by selecting the desired unit from the drop-down menu next to the input fields.

4. What if I enter invalid inputs?

If you enter invalid inputs, such as non-numerical characters or negative numbers, the program will display an error message and prompt you to enter valid inputs.

5. Is the C++ Super Simple Download Calculator accurate?

The C++ Super Simple Download Calculator provides an estimate based on the user's inputs. The accuracy of the calculation depends on the accuracy of the inputs provided. It does not account for external factors that may affect download speed, such as network congestion or file compression.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top