View Single Post
  #1 (permalink)  
Old August 22nd, 2004, 04:14 PM
Jakeyboy Jakeyboy is offline
Registered User
 
Join Date: Aug 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jakeyboy Send a message via Yahoo to Jakeyboy
Default C++ Beginner - help with input types

Hi, I'm new to this forum, so hi!

I'm a total beginner and I thought it would help my programming to write a program with a purpose, not just to say "hello world". As I'm good at Maths and have a good understanding of algebra, I thought I'd make a maths toolkit sort of thing...I'm taking it one step at a time, and i'm working on a simple converter for inches and centimetres. I've done the main part of the program (its console based at the moment, I need to learn some more windows programming before I venture there). The problem I have is, when there is a call for input (at the menu for instance, "enter 1 for this" etc.) if any key other than a number is pressed it skips to another part of the program and messes itself up. the only solution I've come up with is to write an If statement as such:

if (x!=1 || x!=2 || x!=3)
{
cout<<"Incorrect data type, please restart the program";
}

I've tried labelling the menu and then pointing it to that after a system("cls") to make it effectively restart, but it just screws itself again. also i can't use the above method of verification when the user is inputting a value for cm or inches. I'm using the Dev-C++ compiler from bloodshed (because its free!). My code is also really messy...I've put quotes at every possible chance...please don't get too angry, I'm doing my best to tidy it up a bit.

Ok, while writing this I tried to compile my code to check it was ok for posting and i now have 5 errors, saying that "cout" and "cin" etc are undeclared...whats going on? arg! maybe someone can help...

edit: fixed the minor errors with the code i think...still problems with input.



Code:
#include <iostream.h>
#include <stdlib.h>

float convertCmIn(float);
float convertInCm(float);
float main()


{
    float cm;     //  introducing the
    float inches; //  variables for cm and inches.
    float x;      //  "x" is the users choice of function
    menu:         
    cout<<"\t\tThe Centimetres/Inches conversion program, v0.1.2";
    cout<<"\n\n1. Convert Cm to Inches";   //choice 1
    cout<<"\n2. Convert Inches to Cm";     //choice 2
    cout<<"\n3. Exit";                     //choice 3
    cout<<"\n\nEnter your choice: ";       //users input here
    cin>>x;                                

   if (x==1)   //checks the users input,i.e. if the user put in "1"
       {          //do everything between the 2 brackets
       system("cls");
       cout<<"Enter a measurement in Centimetres to be converted to Inches: ";
       cin>>cm;    //the measurement in centimetres
       inches = convertCmIn(cm);   //calls the function to convert
       cout<<"\nHere's the measurement in inches: ";
       cout<< inches << endl;   //returns the value
       cout<< "\n\ncase1" << endl;
       system ("pause"); //"press any key to continue" (gives em a chance to view the output)
       system("cls"); //this clears the screen
       goto menu;    //go back to the menu
       }


   else if (x==2)  //see case one
       {
       system("cls");
       cout<<"Enter a measurement in inches to be converted to Centimetres: ";
          cin>>inches;

          cm = convertInCm(inches);

          cout<<"\nHere's the measurement in inches: ";
          cout<< cm << endl;
       cout<<"\n\ncase2" << endl;
       system ("pause");
       system("cls");
       goto menu;
       }


         else if (x==3)    //again, checking input
         return 0;         //return nothing, thereby ending the program (i think)

        else
        {
        cout<<"\n\n\tYour input was not valid, please restart the program\n\n";
        system("pause");
        return 0;                     //clear the screen.
        }

}

float convertCmIn(float cm)
      {
         float inches;
         inches = cm/2.54;
         return inches;

      }

float convertInCm(float inches)

      {
         float cm;
         cm = inches*2.54;
         return cm;

      }
      //end




Jakeyboy - Annoying Maths Teachers from the comfort of his caravan
Reply With Quote