-
October 12th, 2010, 11:25 AM
#1
Help with building functions
Hello, I am new to the fourms and would like to see if anyone can help me successfully build this program. I have to use my existing program and build 4 functions, use a loop, and use a switch statement. The loop I will use will be to run the program until the user types the letter q. My old code is here:
//OSPF cost Calculator. This program calculates the cost by taking either K //or M values.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int intNUM;
int intWhole;
double output=0;
const float Ref_BW = 100000000;
string userInput;
string strNumber;
string strLetter;
double intFinalNumber;
//Declared integers, doubles, const float, and //strings
cout << "Enter Bandwidth (K/M):" << endl;
// Prints "Enter Bandwidth to the screen
cin>>userInput;
// Makes string "userInput" the userInput
intNUM = userInput.length() -1;
// intNUM equals the userInput length minus one
strNumber = userInput.substr(0, intNUM);
//Makes substring called "strNumber" from beginning //(0) to include everything
//but the number by including "intNum" (which doesn't //include the letter)
intWhole = userInput.length();
//Takes the Whole Length of the string
strLetter = userInput.substr((intWhole -1),1);
//Made sub string which took the whole length of the //string minus 1 and then
//included the last value (which is the letter)
intFinalNumber = atof(strNumber.c_str());
//Converts Num to a double called intFinalNumber
if (strLetter == "K")
{
intFinalNumber = (intFinalNumber * 1000);
output = (Ref_BW / intFinalNumber);
cout << "Your Result is" << output << endl;
}
// Takes the intFinal number times 1000 then divides //it by the Ref_BW to
// get the output
else if (strLetter == "M")
{
intFinalNumber = (intFinalNumber * 1000000);
output = (Ref_BW / intFinalNumber);
cout << "Your Result is" << output << endl;
}
// Takes the intFinal number times 1000000 then //divides it by the Ref_BW to
// get the output
else
{
cout << "Invalid Entry" << endl;
}
//If a K or M is not entered it will print "Invalid //Entry" to the screen.
return 0;
}
Basically the code takes user input and parses the last letter.. checks if it is a "K" or "M" and calculates it. I tried to change the if/else statements to switch statements but was unsuccessful because switch only takes int values. I am very confused on how to build functions out of this program and any help would be appreciated. Thanks in advance
-
October 12th, 2010, 12:21 PM
#2
Re: Help with building functions
Change strLetter to a char and compare strLetter to char literals such as 'K', and your switch will work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|