Solved

Looping ????

Posted on 2004-09-18
3
241 Views
Last Modified: 2010-04-01
I  have used looping in C++ before, HOWEVER, I am learning to use public and private classes. If I write my program without class I have no problem using a "do/while" or "if /else" statements. Is the looping funtion different when using public and private class?  I am going to post the code that I am working with below. My request is that you don't fix my code, but add a comment(s) that helps me to learn. If I dont get It I will repost with a more specific question. Thank you much!!!

#include <iostream>

using namespace std;
class temp
{
private:

    static float num1;
    static float num2;
    double numF;
    double numC;

public:

    void inputTempF();
    void calcTemp();
    void displayTempC();
};

float temp::num1 = .555555;
float temp::num2 = 32;

main()
{
temp theTemp;
do
{
theTemp.inputTempF();
theTemp.calcTemp();
theTemp.displayTempC();
}

while
(theTemp.inputTempF()!= 9999);
system("Pause");

}

void temp::inputTempF()
{
    cout << "Please enter a temp in F: ";
    cin >> numF;
    cout << endl;
}

void temp::calcTemp()
{
          numC =(num1*(numF-num2));
}

void temp::displayTempC()
{
cout << "The C temp is "<< numC ;
          cout << endl;
}
0
Comment
Question by:bananaamy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 250 total points
ID: 12093059
#include <iostream>

using namespace std;
class temp
{
private:

    static float num1;
    static float num2;
    double numF;
    double numC;

public:

    void inputTempF();
    void calcTemp();
    void displayTempC();
};

float temp::num1 = .555555;
float temp::num2 = 32;

main()
{
temp theTemp;
/* This is a problem in that each time through the loop you will read input from the user
     twice, once in the beginning of the boolean expression, the other is in top of the do loop.

     Probably want to use a while() ... loop.
*/
do
{
theTemp.inputTempF();
theTemp.calcTemp();
theTemp.displayTempC();
}

while
(theTemp.inputTempF()!= 9999);
system("Pause");

}

void temp::inputTempF()
{
    cout << "Please enter a temp in F: ";
    cin >> numF;
    cout << endl;
}

void temp::calcTemp()
{
          numC =(num1*(numF-num2));
}
 
void temp::displayTempC()
{
cout << "The C temp is "<< numC ;
          cout << endl;
}


num1 and num2 are the worst possible names for those two constants.  

-bcl
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 12093060
Hi again,

The error in your code is in the 'while' portion. Your are evaluating the return of inputTempF() but it returns nothing!
Even if it returns the value of 'numF', you are calling this function twice (at the beginning of loop, and at the while statement.
In this case, a better aproach will be implement an 'infinite' loop like this:

while (true) {
    // ask for input
    // evaluate if it is 9999
    // if yes, break;
    // convert
    // display result
}

to acomplish that you will need inputTempF() function to return a value:

float temp::inputTempF()
{
    cout << "Please enter a temp in F: ";
    cin >> numF;
    cout << endl;
    // return the value of numF here
}

Remember to make changes in both class definition and function declaration.

Good luck,
Jaime.
0
 

Author Comment

by:bananaamy
ID: 12093083
Thankyou both- Please watch for a repost, because I am not sure I understand. But first I will try to implement your sugestions.  Thank-you much
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question