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

  • Context: C/C++ 
  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    C++ Calculator
Click For Summary

Discussion Overview

The discussion revolves around a C++ program designed to calculate download times based on user-selected internet speeds (fast or slow). Participants address issues with the program's conditional statements and offer suggestions for improvement, focusing on coding practices and user experience.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant identifies a logical error in the if statement, explaining that the condition is always true due to incorrect syntax.
  • Another participant clarifies that the original if statement evaluates a string incorrectly, suggesting a corrected version for proper functionality.
  • Suggestions include using character comparisons instead of strings, such as checking the first character of the input or using the strcmp function.
  • Some participants propose simplifying the program by removing the need for user input regarding speed, suggesting that both download times could be displayed simultaneously.
  • A participant expresses a need for resources for beginner programmers in C++ and C#, seeking guidance on where to find appropriate materials.

Areas of Agreement / Disagreement

Participants generally agree on the issues with the if statement and offer various solutions. However, there is no consensus on the necessity of user input for speed selection, with differing opinions on program design and user experience.

Contextual Notes

Some suggestions involve changing data types from strings to characters, which may impact how the program is structured. The discussion also highlights the importance of clear coding practices and user-friendly design.

Who May Find This Useful

Beginner programmers seeking to improve their understanding of C++ syntax and logic, as well as those interested in best practices for user interface design in programming.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K