Simple Loop Issue

Ok, this is my first post, be easy. I'm quite new to c++ programming. I am trying to make a simple feet to inches converter. It works perfectly except for when I try to to loop it. I've tried using a while loop (for when feet >=0) around the function calls but it tells me feet is not defined in the scope. How can I go about looping this program until user types in a negative number.


Additional Information: I haven't set feet equal to any number once it first gets to the while loop, so I'm thinking that's the problem. However, I can't set it equal to anything in the class or before the loop without getting more errors.


Here is the code:


#include <iostream>
#include <string>
using namespace std;

template <class Q>
class conversion{
public:
void convertToInches();
void enterFeet();
private:
Q feet;
Q inches;

};

int main(){

conversion <double> obj;

while(feet >=0){
obj.enterFeet();
obj.convertToInches();
}
}

template <class Q>
void conversion<Q>::enterFeet(){
cout << "Welcome to the feet to inches converter.\nEnter how many feet you would like to be converted:\n";
cout << "(Enter negative number to quit)\n";
cin >> feet;
}

template <class Q>
void conversion<Q>::convertToInches(){
inches=feet*12;
cout << inches;
}
closed account (3CXz8vqX)
O.o surely this could have been done much more easily.

Why are you using a template? Why not just a full blown 'class'.

Wrap in code tags please.

Welcome!

1
2
3
4
5
6
7
 // inside while should be accessor func obj.getFeet( )
while(feet >=0)
  {
    obj.enterFeet();
    obj.convertToInches();
  }
}


Two things:
(1) I believe the feet you're trying to to use is in the object: conversion <double> obj, so you need to use obj.feet instead of just feet.

(2) Unfortunately you cannot use obj.feet since you declared it as private in your class. To solve this I would recommend creating an accessor function. You can always change feet to be public, but that would go against the purpose of classes.
Topic archived. No new replies allowed.