Mitsakos (320) Jul 28, 2008 at 5:35pm
try:
if(pick == "withdrawl"){
you can aply directly the operators +, >=, ==, <= etc to strings...
If you want to use strcmp() use pick.c_str()
strcmp() works only with c type strings.
// Lab1 Exercise 3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using std::cin;
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
double celcius, fahr, counter, stemp, etemp, inc;
char celkey[] = "celcius";
char fahrkey[] = "fahr";
char CoFinput[255];
//declare doubles and chars
cout << "Temperature Conversion Table Software:" << endl << endl << "Do you want to convert Celcius Temperatures or a Fahrenheit Temperatures?";
cout << endl << "Enter \"celcius\" or \"fahr\": ";
cin >> CoFinput;
//ask users for conversion style
for (counter = 1; counter != 0;) //for loop used to test whether if it's Celcius Fahrenhiet or an invalid input
{
if(strcmp(CoFinput, celkey) == 0 || strcmp(CoFinput, fahrkey) == 0) //if user input for CoFinput is celsius or fahr, it runs the if else statement below
{
if(strcmp(CoFinput, celkey) == 0) //if it's celcius, it runs the code below
{ //the code below ask for the starting and ending temperature
cout << "Enter the starting temperature: ";
cin >> stemp;
cout << "Enter the ending temperature: ";
cin >> etemp;
while (stemp > etemp) //this while loop checks starting and ending temperature mistakes
{
cout << "Enter a starting temperature that is lower than the ending temperature." << endl;
cout << "Enter the starting temperature: ";
cin >> stemp;
cout << "Enter the ending temperature: ";
cin >> etemp;
}
//the code below ask for hte increment to be added
cout << "Enter the incremental factor (do not use a value <= 0): ";
cin >> inc;
while (inc <= 0) //this while loop checks to be sure the increment isn't a negative or zero
{
cout << "Enter a valid value for the incremental factor: ";
cin >> inc;
}
celcius = stemp; // the computing will use the starting temp
cout << "\nCelcius Temperature \t \t Fahrenheit Temperature \n";
cout << "_______________________________________________________\n";
while (celcius <= etemp) //the while loop will continue to do the converting until the temperature goes pass the ending temperature
{
fahr = celcius * (9.0/5.0) + 32;
cout << celcius << "\t \t \t \t " << fahr << endl;
celcius = celcius + inc;
}
counter = 0;
}
if(strcmp(CoFinput, fahrkey) == 0) //read the comments above to understand the following code, it's the same thing
{
cout << "Enter the starting temperature: ";
cin >> stemp;
cout << "Enter the ending temperature: ";
cin >> etemp;
while (stemp > etemp)
{
cout << "Enter a starting temperature that is lower than the ending temperature." << endl;
cout << "Enter the starting temperature: ";
cin >> stemp;
cout << "Enter the ending temperature: ";
cin >> etemp;
}
cout << "Enter the incremental factor (do not use a value <= 0): ";
cin >> inc;
while (inc <= 0)
{
cout << "Enter a different value for the incremental factor: ";
cin >> inc;
}
fahr = stemp;
cout << "\nFahrenheit Temperature \t \t Celcius Temperature \n";
cout << "_______________________________________________________\n";
while (fahr <= etemp)
{
celcius = (5.0/9.0) * (fahr-32);
cout << fahr << "\t \t \t \t " << celcius << endl;
fahr = fahr + inc;
}
counter = 0;
}
}
else //this ask the user to input the correct string, "celcius" or "fahr"
{
cout << endl << "Invalid Input! Please enter a valid string." <<endl;
cout << "Enter \"celcius\" or \"fahr\": ";
cin >> CoFinput;
}
}
return 0;
}
thx, zhuge helped me fix the code and I finished my HW. I only have one problem that I can't seem to understand. If I ask it to start at 0 to 1 in increments of .1, it goes from 0 to 1, but if I ask it to do increments of .01, it goes from 0 to .99, not 0 to 1