• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Noob needs help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
ok using visual basic 2010 .. everything seems to look right to a noob like me but what id like to happen is to keep the program running ..until the exit command is used.

EDIT:

1. When entering correct numbers and options it works fine.
2. when entering an invalid character it jumps to error and would you like to continue. but even if you press Y it ends.
3. also after choosing option and entering a number to be converted. If i enter a 'J' instead of a number it will infinite loop ..
4. What am i doing wrong?
5. Anything else to help me get pointed in the right direction.

oh and im trying "do while" loops because thats the section we are in and he wants us to be able to be versatile with all loops doing different things ..

here is the copy of the cpp file .. simple text












///////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <conio.h>
#include <cmath>
#include <iomanip>



using namespace std;


int main ()
{

double celcius, fahrenheit;
string exit;
char choice;

cout << "This program is for converting Fahrenheit to Celsius or Celsius to Fahrenheit " << endl << endl;

do
{


cout << " \nPress 1 to convert Fahrenheit to Celsius " << endl << endl;
cout << "\nPress 2 to convert Celcius to Fahrenheit " << endl << endl;
cin >> choice;

if (choice == '1')
{
cout << "\nThis will convert Fahrenheit to Celcius " << endl << endl;
cout << "\nPlease enter a number to be converted" << endl << endl;
cin >> fahrenheit;

celcius = (fahrenheit - 32) * 5/9;

cout << "\nThe number you entered was " << fahrenheit << "\nThat number converted to Celcius is " << celcius << endl << endl;

}
else if (choice == '2')
{
cout << "\nThis will convert Celcius to Fahrenheit " << endl << endl;
cout << "\nPlease enter a number to be converted" << endl << endl;
cin >> celcius;

fahrenheit = celcius * 9/5 + 32;

cout << "\nThe number you entered was " << celcius << "\nThat number converted to Fahrenheit is " << fahrenheit << endl << endl;

}
else
{
cout << "\nYou entered an invalid number ......" ;
cout << "\n";
}

cout << "\nWould you like to continue ? Please press Y to continue or any other key to exit.... " << endl << endl;
cin >> exit;
}
while ((exit == "y") || (exit == "Y"));

return 0;


}


////////////////////////////////////////////////////////////////////////////////////////////////////////
 
Last edited:
You should talk to your instructor. If you guys aren't doing input validation, ignore putting in improper values. Most programming introduction classes assumes the "end user" enters proper information.

Why are you using string for "exit"? You should use boolean for this.

Besides that, it looks fine.
 
ok i fixed the invalid character issue .. i had choice as an int and not char <----- noob :D


and well im a noob ... i will look into the boolean as we speak ..
 
Since you have it written, here is the version that I did in my intro class.

Code:
 #include <iostream>
 #include <iomanip>
 using namespace std;
 

 int main(void)
 {
         //Declare variables
         double f = 0;
         double c = 0;
         char convertto = '0'; //Set to invalid option for error detection
 

         //Explain program
         cout << "This program will convert temperatures between degrees Fahrenheit and degrees Celcius." << endl;
         cout << "It will take decimal values and round to the nearest tenth of a degree." << endl;
 

         //Get input
         cout << "Would you like to convert from Fahrenheit or Celcius (Enter C or F): ";
         cin >> convertto;
         cout << endl;
 

         //Evaluate input
         if ( convertto == 'F' || convertto == 'f' ) //If Fahrenheit
         {
                 cout << "Please type in the temperature, in Fahrenheit: ";
                 cin >> f;
                 c = (f-32)/1.8;
                 cout << endl << f << " degrees Fahrenheit is " << fixed << setprecision (1) << c << " degrees Celcius." << endl;
         } //fi
         else if ( convertto == 'C' || convertto == 'c' ) //If Celcius
         {
                 cout << "Please type in the temperature, in Celcius: ";
                 cin >> c;
                 f = (c*1.8)+32;
                 cout << endl << c << " degrees Celcius is " << fixed << setprecision (1) << f << " degrees Fahrenheit." << endl;
         } //fi
         else //If invalid input
         {
                 cout << "Invalid input. You did not select \"F\" or \"C\"." << endl;
         } //fi
 

         return 0;
 } //main()
 
ok i loaded your program and built it and ran it ...

yours seems to have the same issue mine has but in a little different way ..

after you choose c or f .. you get the option to enter a number to convert ....

so at that prompt i enter the letter J .. it converts it by acii value and program completes ..

how to you only except a number for input ? rejecting letters or symbols ?


if i could figure that out my program would be 100 % then ..
 
Again, that is a question for your instructor. Input validation probably isn't something you should worry about now. You are working on the basics.

Without using extra libraries besides "string", you could import the value as a string and either strip out invalid characters or prompt the user to enter it again.
 
I try to use strings for input characters. Just use string.at(0)=='char'; this pretty much ensures that you cant invalidate the IOstream. Conversion of numbers inside of a string is either very simple or horribly complex depending on your programs required accuracy.
Code:
//given nums is a string of ints returns int
cin << nums;
for (int i=0;i<nums.length();i++){
  if (int(nums.at(i))<48 || int(nums.at(i))>57){
    cout << "\nYou have entered an invalid number\n";
    eixt(1);
  }
}
int number=0;
int pos=nums.length()-1;
for (int i=0; i<nums.length();i++){
  int decimal=10*i;
  if (decimal<1) { decimal=1; }
  number+=(int(nums.at(pos))-48)*decimal;
  pos--;
}
 
Last edited:
thanks for reply

so i would just copy the for loop into the program ?? if so where would it get entered ?
 
That goes in the place of the int your using to catch the user input before the calculation happens.

The first loop integrity checks the input to make sure that it is only numbers. The second converts the string values of the number into int.

Note, I just noticed that loop is running backwards with its incrementation. Ill fix it RQ.
Note 2, Also this same method can be used for non int numbers BUT it requires another loop to process the numbers behind the decimal point.
 
well is this real advanced stuff ??? cuz if it is i may be best off waiting like thideras suggest
 
Its not really advanced but it can cloud the basics. If you're having a hard time with a looping function, set a break point and step through the program (I noticed today that the VS2008/2010 start page have a basic debugging guide video), making sure the values in your variables make sense.

A lot of times with "menu" driven CLI programs I find it easier to break the program up into a couple functions. The following is in psudocode and assume you know how to write your own functions in C++ but it'll give you an idea.

Code:
//Name, date, documentation hopefully

//#includes statements

//function prototypes

int main()
{
    std::string Choice = "0";

    do
    {

     std::cout << "Blah blah blah Degrees C: C" << std::endl << "blah blah blah Degrees F: F" << std::endl << "Choice: ";
     std::cin >> Choice;

    }while(ChoiceIsInvalid(Choice));

    if (Choice == "C" || Choice == "c")
    {
        CCalc();
    } 
    else 
    {
        FCalc();
    }

    return 0;
}

bool ChoiceIsInvalid(std::string Choice)
{

    bool retVal = true;

    if (Choice == "C")
    {
        retVal = false;
    }
    else if (Choice == "c")
    {
        retVal = false;
    }
    else if (Choice == "F")
    {
        retVal = false;
    }
    else if (Choice == "f")
    {
        retVal = false;
    }

    return retVal;
}

void CCalc()
{
    //Perform Q/A for converting between degrees F and degrees C
}

void FCalc()
{
    //Perform Q/A for converting between degrees C and degrees F
}

If you don't know how to write your own functions yet, stick to just breaking your program into smaller bits and make sure the input works properly. Try removing the math parts and just work with the code to get C, c, F, or f and handle the erroneous states. Then add the math back in.
 
thanks very much .. we covered loops ..and we just now are getting more into functions .. really liking this class !! cant wait till i can actually do something i want :D
 
Back