Do while loop not working nor is if else statement

Hello!

I could use some help trying to figure out why my loop is not working. The program runs, but it doesn't try to use the loop and run again. Does anyone know why?

Also I can not figure out why my if else statement is defaulting to my else and that's all.

Thank you in advance.


#include <iostream>
#include <cstdlib>
using namespace std;



int main()
{

char day, playagain;
int duration, callstart;

do //where loop starts
{
cout << "This is your long distance phone call cost calculator.\n"; // Inform user what is expected of them
cout << "Before any numbers are crunched some data will need to be collected: \n";
cout << "You will have to provide the day of the week the call was made.\n";
cout << "The time the call started, and the length of the call\n";
cout << "To enter a day use the following abbreviations:\n";
cout << "Su Mo Tu We Th Fr Sa\n";
cout << "Time will be entered using a 24 hour system, also know as military time.\n";
cout << "For example, 1:30pm is now going to be 1330\n";

// Collect data from user to be used in if else statement

cout << " Enter the time the call started\n";
cin >> callstart;
cout << "Enter the length of the call in minutes\n";
cin >> duration;
cout << "Enter the day of the week of the call\n";
cin >> day;

// Write if else statement, allowing for uppper and lower case entries

if (day == 'Su' || day == 'su' || day =='sU' || day == 'Sa' || day == 'sa' || day == 'sA')
cout << "The cost is: " << (duration *.15) << endl;
else if (callstart >= 8 && callstart <= 18)
cout << "the cost is: " << (duration * .40) << endl;
else
cout << "the cost is: " << (duration * .25) << endl;

cout << "Would you like to calculate another call?\n";
cin >> playagain;


} while (playagain == 'Y' || playagain == 'y' );


system("pause");
return 0;
}
Hi,

Welcome to cpluplus :+)

A char type can only hold 1 char, so that is why your if statement fails. You could use std::string instead, or get the user to select a day by number.

With the looping, search on this site for bool controlled while loop to see different examples on how to do this.

Btw, it pays to always use braces in any type of loop, if or switch statement - even if there is only 1 LOC, it will save you one day when yo add more code.

Try to avoid having "magic numbers" like 0.15, 18, 0.25 in your code make them const variables (double or unsigned) instead.

Your code does not take into account of times entered - you have :

else if (callstart >= 8 && callstart <= 18)

Presumably this is meant to be 0800 and 1800, but note that 0800 is an octal number. Should you prompt for hours and minutes separately?

In future, can you always use code tags? Select your code, then press the <> button on the format menu.

Hope all goes well :+D
Last edited on
Topic archived. No new replies allowed.