LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Do while problem (https://www.linuxquestions.org/questions/programming-9/c-do-while-problem-705804/)

b3n 02-19-2009 12:37 AM

C++ Do while problem
 
Hello everyone,
this is what my program is supposed to do: initially prompts the user for the area code. Then the program continuously accepts phone call data, and displays data for any phone call to or from the specified area code. The program ends when -1 is input for the area code.
Code:

#include <iostream>
using namespace std;
const float LOW_RATE=0.10;
const float HIGH_RATE=0.13;
const float TIME_LIMIT=20;
int
main()
{
      int givenAreaCode;
      int custAreaCode;
      int custPhoneNum;
      int calledAreaCode;
      int calledPhoneNum;
      float minutes;
      float price;
   
      cout << "Please enter area code: ";
      cin >> givenAreaCode;
   
      cout << "Please enter your area code: ";
      cin >> custAreaCode;
   
      cout << "Please enter your Phone Number: ";
      cin >> custPhoneNum;
     
      cout << "Please enter called area code: ";
      cin >> calledAreaCode;
   
      cout << "Please enter called number: ";
      cin >> calledPhoneNum;
   
      cout << "Minutes: ";
      cin >> minutes;

   
do {
        if (custAreaCode = -1){
        }else{
              if (custAreaCode == givenAreaCode || calledAreaCode == givenAreaCode)
                  if (custAreaCode!= calledAreaCode && minutes > TIME_LIMIT)
                  price = minutes * LOW_RATE;
                  else
                  price = minutes * HIGH_RATE;               
     
      cout  << " your area code is " << custAreaCode << endl;
      cout  << " your phone number is " << custPhoneNum << endl;
      cout  << " your called area code is " << calledAreaCode << endl;
      cout  << " your called number is " << calledPhoneNum << endl;
      cout  << " your price is " << price << endl;
}
}
      while (custAreaCode = -1);

      cout << "Please enter area code: ";
      cin >> givenAreaCode;
   
      cout << "Please enter your area code: ";
      cin >> custAreaCode;
   
      cout << "Please enter your Phone Number: ";
      cin >> custPhoneNum;
     
      cout << "Please enter called area code: ";
      cin >> calledAreaCode;
   
      cout << "Please enter called number: ";
      cin >> calledPhoneNum;
   
      cout << "Minutes: ";
      cin >> minutes;

cin.get();
cin.ignore();
return 0;
}

the do while has problems. it COMPILES but doesnt work.
any thoughts?

wje_lq 02-19-2009 02:30 AM

Quote:

any thoughts?
Sure. It would help us help you if you told us:
  1. exactly what you expect the program to do; and
  2. what the program did instead.
You did well by posting the entire program in [CODE] markers, but you should also copy and paste your actual output the same way.

In fact, even doing this for yourself (before you contact the forum) is a good way to get a handle on the problem.

Maligree 02-19-2009 03:28 AM

Code:

while (custAreaCode = -1);
Code:

if (custAreaCode = -1){
You're assigning cusAreaCode the value -1. You have to use == when comparing two values.

johnsfine 02-19-2009 07:20 AM

Maligree explained the major bug, but you should also look at
Quote:

Originally Posted by b3n (Post 3449161)
Code:

  if (custAreaCode == givenAreaCode || calledAreaCode == givenAreaCode)
      if (custAreaCode!= calledAreaCode && minutes > TIME_LIMIT)
      price = minutes * LOW_RATE;
      else
      price = minutes * HIGH_RATE;


When the first of those two if statements is false, what did you intend the price to be?

I don't think your code does what you intend.

metrofox 02-19-2009 07:49 AM

The compiler compiles as it has to do because there are not syntax problems and it does its job, but you wronged the symbols, you seem confused about the sign " = " and the sign " == " and wronging these symbols you won't get any error if not logic ones and wrong results ;) Correct the symbols, it might work.

ErV 02-19-2009 07:56 AM

Quote:

Originally Posted by Maligree (Post 3449321)
Code:

while (custAreaCode = -1);
Code:

if (custAreaCode = -1){
You're assigning cusAreaCode the value -1. You have to use == when comparing two values.

And program will hang (even if this bug is fixed) due to the infinite loop when user give -1 for custAreaCode.

Maligree 02-19-2009 08:34 AM

I did not look at the whole program before, just found those (obvious) comparison/assignment bugs. Even now, I'm not going to fix the whole thing, since a) I don't know how it is supposed to work and b) I'm not really into doing other peoples' homework.

Anyway, the structure is really wrong. I can't even elaborate, the use of that do { } while is.. *cough*. And at the end you prompt for more data and just discard it.*

All in all, I guess you should really read up on how loops and conditional checks work.

*- reminds me of "What does your robot do?" "It collects data about the surrounding environment, then discards it and drives into walls".


All times are GMT -5. The time now is 06:23 AM.