Preventing letter/symbol input
Feb 14, 2013 at 2:54pm UTC
Hi, basically trying to stop letters and symbols being entered into my main function. For the menu section I used a buffer yet I try this with my function and its not working. Any help would be great appreciated and it is for the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <iostream>
#include <string>
#include <math.h>
#include <windows.h>
using namespace std;
struct Point3D
{
float x;
float y;
float z;
} V1, V2;
double CalculateDistance(Point3D * V1, Point3D * V2);
double CalculateLenth(Point3D* * V1, Point3D* * V2);
int main(void )
{
string buffer;
char choice; // variables
double distance;
double length;
do
{
do
{
system("CLS" );
cout << "Welcome to the Distance Calulator for 3D points" << endl; //main menu
cout << "1. Go" << endl; //commence distance calculating
cout << "2. Quit" << endl; //quit the application
cin >> buffer;
} while (buffer.length() > 1); //limiting the user to enter 1 numerical character to being the application
choice = buffer[0];
if (choice == '1' )
{
cout << "Enter Vertex 1's X co-ordinate: " ;
cin >> V1.x;
cout << "Enter Vertex 1's Y co-ordinate: " ; // Vertex 1's co-ordinates
cin >> V1.y;
cout << "Enter Vertex 1's Z co-ordinate: " ;
cin >> V1.z;
cout << "Enter Vertex 2's X co-ordinate: " ;
cin >> V2.x;
cout << "Enter Vertex 2's Y co-ordinate: " ; // Vertes 2's co-ordinates
cin >> V2.y;
cout << "Enter Vertex 2's Z co-ordinate: " ;
cin >> V2.z;
distance = CalculateDistance(&V1, &V2);
cout << "Distance between V1 and V2 = " << distance << endl; // outputting the total distance
cout << " " << endl;;
cout << "Do you want to calculate another distance ?" << endl; // option to carrying on the application
cout << "Press '1' to go back to the main menu, or '2' to quit." << endl;
cin >> choice;
}
} while (choice != '2' ); //the start of the sequence if the user chooses to end the application
cout << "Goodbye" ;
Sleep(500); // makes the computer 'wait' for 500 milli-seconds
return 0; // end the application
}
double CalculateDistance(Point3D * V1, Point3D * V2)
{
double distance;
distance = sqrt(pow(V1 -> x - V2 -> x, 2) +
pow(V1 -> y - V2 -> y, 2) + // distance calculator for the first function
pow(V1 -> z - V2 -> z, 2));
return distance;
}
Topic archived. No new replies allowed.