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
Solved

Looping ????

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

860 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