LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2009, 12:37 AM   #1
b3n
LQ Newbie
 
Registered: Apr 2007
Posts: 20

Rep: Reputation: 0
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?
 
Old 02-19-2009, 02:30 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
Old 02-19-2009, 03:28 AM   #3
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Code:
while (custAreaCode = -1);
Code:
if (custAreaCode = -1){
You're assigning cusAreaCode the value -1. You have to use == when comparing two values.

Last edited by Maligree; 02-19-2009 at 03:30 AM.
 
Old 02-19-2009, 07:20 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Maligree explained the major bug, but you should also look at
Quote:
Originally Posted by b3n View Post
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.
 
Old 02-19-2009, 07:49 AM   #5
metrofox
Member
 
Registered: Jan 2009
Location: Palermo, Italy
Distribution: Slackware
Posts: 236

Rep: Reputation: 37
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.
 
Old 02-19-2009, 07:56 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by Maligree View Post
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.
 
Old 02-19-2009, 08:34 AM   #7
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
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".

Last edited by Maligree; 02-19-2009 at 08:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration