Solved

Looping ????

Posted on 2004-09-18
3
235 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 clear a vector as well as how to detect empty vectors in C++.

743 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