Solved

Looping ????

Posted on 2004-09-18
3
236 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now