Input Validation error in C++

I need help with a input validation code.

Page 1 of 1

1 Replies - 1530 Views - Last Post: 17 June 2009 - 08:41 AM Rate Topic: -----

#1 turner2108  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 28-January 09

Input Validation error in C++

Post icon  Posted 17 June 2009 - 08:32 AM

I need help with a code that will send an error message saying the user can not input a number with the last 2 digits being greater than .59 for the minutes part of start_Time.




#include <iostream> // Setup standard output.
#include <iomanip> // Required for numeric formatting function.
using namespace std;

// Marks standard function.
int main()
{

// Declaration of variables and initialization.
const double RATE_PER_MIN1 = .12, // Rate per minute between 00.00 - 06.59.
RATE_PER_MIN2 = .55, // Rate per minute between 07.00 - 19.00.
RATE_PER_MIN3 = .35; // Rate per minute between 19.01 - 23.59.

double start_Time, // Starting time of call.
num_Minutes, // Number of minutes of call.
charges; // Total long distance charge.


// Name of company.
cout << "Long Distance Calls " << endl;
cout << "------------------- " << endl;

// Seperates lines for easier readability.
cout << endl;

// Allows user to input the starting time.
cout << "Please enter the starting time of ";
cout << "the call in the format of HH.MM." << endl;
cin >> start_Time;
if (start_Time > 23.59) // Input validation.
{ // Starting time cannot be more than 23.59.
cout << start_Time << ": ";
cout << "This entry is invalid, the start time can not be greater than 23.59.";
cout << "Please run the program again and enter a valid number." << endl;
}

else
// Allows user to input minutes of call.
{cout << "Please enter the number of minutes of the call. " << endl;
cin >> num_Minutes;
}

// Seperates lines for easier readability.
cout << endl;

// Set numeric output formatting.
cout << fixed << showpoint << setprecision(2);

// Rate per minute 1.
if (start_Time >= 00.00 && start_Time <= 06.59)
{ charges = num_Minutes * RATE_PER_MIN1; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges;
}
// Rate per minute 2.
else if (start_Time >= 07.00 && start_Time <= 19.00)
{ charges = num_Minutes * RATE_PER_MIN2; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges;
}
// Rate per minute 3.
else if (start_Time >= 19.01 && start_Time <= 23.59)
{ charges = num_Minutes * RATE_PER_MIN3; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges;
}

// Seperates lines for easier readability.
cout << endl;

// Stops the program from closing automatically.
system("pause");
return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Input Validation error in C++

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Input Validation error in C++

Posted 17 June 2009 - 08:41 AM

your code post right
#include <iostream> // Setup standard output.
#include <iomanip> // Required for numeric formatting function.
using namespace std;

// Marks standard function.
int main()
{

// Declaration of variables and initialization.
const double RATE_PER_MIN1 = .12, // Rate per minute between 00.00 - 06.59.
RATE_PER_MIN2 = .55, // Rate per minute between 07.00 - 19.00.
RATE_PER_MIN3 = .35; // Rate per minute between 19.01 - 23.59.

double start_Time, // Starting time of call.
num_Minutes, // Number of minutes of call.
charges; // Total long distance charge.


// Name of company.
cout << "Long Distance Calls " << endl;
cout << "------------------- " << endl;

// Seperates lines for easier readability.
cout << endl;

// Allows user to input the starting time.
cout << "Please enter the starting time of ";
cout << "the call in the format of HH.MM." << endl;
cin >> start_Time;
if (start_Time > 23.59) // Input validation.
{ // Starting time cannot be more than 23.59.
cout << start_Time << ": ";
cout << "This entry is invalid, the start time can not be greater than 23.59.";
cout << "Please run the program again and enter a valid number." << endl;
}

else
// Allows user to input minutes of call.
{cout << "Please enter the number of minutes of the call. " << endl;
cin >> num_Minutes;
}

// Seperates lines for easier readability.
cout << endl;

// Set numeric output formatting.
cout << fixed << showpoint << setprecision(2);

// Rate per minute 1.
if (start_Time >= 00.00 && start_Time <= 06.59)
{ charges = num_Minutes * RATE_PER_MIN1; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges; 
}
// Rate per minute 2.
else if (start_Time >= 07.00 && start_Time <= 19.00)
{ charges = num_Minutes * RATE_PER_MIN2; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges;
}
// Rate per minute 3.
else if (start_Time >= 19.01 && start_Time <= 23.59)
{ charges = num_Minutes * RATE_PER_MIN3; // Calculates charges.
cout << "The long distance charge: "; // Displays charges.
cout << "$ " << charges;
}

// Seperates lines for easier readability.
cout << endl;

// Stops the program from closing automatically.
system("pause");
return 0;
} 




right off the bat theres a lot of places where you put a comma instread of a
semi colon:
// Declaration of variables and initialization.
const double RATE_PER_MIN1 = .12, //<---
RATE_PER_MIN2 = .55, //<---
RATE_PER_MIN3 = .35; // Rate per minute between 19.01 - 23.59.

double start_Time, //<---
num_Minutes, //<---
charges; // Total long distance charge.


This post has been edited by ImaSexy: 17 June 2009 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1