EOSERV Forum > Lounge > New to C++
Topic is locked.
Page: << 1 >>
New to C++
Author Message
Post #90474 New to C++

hi , i've recently learned c++ abit. I made a Temperature converter program in it.. My problem is when i run it and choose option ( 1 or 2) and put the values and get the answer the program closesit self.. It doesn't show Choose Option after that. Please help :/

here is the code : 

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

float f,c

double option;

cout << " choose option 1 = f to c , 2 = c to f " << endl;

cin >>option;


if (option == 1)

{

cout << " enter temp of farenheit";

cin>>f;

c=(f-32)*0.55;

cout << " temp in celsius is = " <<c;

getch();

}


else if (option == 2)

{

cout << " enter temp in celsius ";

cin>>c;

f=(f+32)*1.8;

cout << " temp in farenheit is = " <<f;

getch();

}

}

13 years, 41 weeks ago
Post #90546 Re: New to C++

What you need to do is use a while loop.

One way to do this is to initialize option to an arbitrary value that isn't 1,2, or your escape code:

char option = 4;

Then you put the rest of the code after that in a while loop. The condition for the loop being that option != 'q', or whatever you want the escape code to be.

So every time it loops, it will ask for the option again. If you input an invalid value, it will ask you for a value again. If you input 1 or 2, it will enter the correct 'if' statement. If you input 'q' or whatever your escape code is, it will break the loop and end the program.

A second way to do this is to not initialize option to anything. Leave it at:

char option;

Then your while loop will encompass the rest of the code. The condition for the while loop will be 1 or true:

while(true)

So the only way to end it will be to use a break statement.

Then, after you cin >> option; You have an if statement that says:

if(option == 'q') //or other escape code
  break;

Both those ways will do the exact same thing. You'll find in programming that you can do things multiple ways, its up to you to decide which way is the best (efficiency etc.). Of course with a basic example like this efficiency doesn't matter, that's just for when you get into data structures.

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
13 years, 41 weeks ago
Post #90815 Re: New to C++

Can you show it by changing the code . im very much new to C++ so i don't get it :S

13 years, 40 weeks ago
Post #91530 Re: New to C++

Well I actually re made this whole thing and when the person is finished using the option 1 or 2 it goes back to the top.

HERE



13 years, 40 weeks ago
Post #91631 Re: New to C++

The way I described is much easier...if you don't understand something, look it up. There are tons of tutorials on C++ basics like while loops.

And how do you expect to learn from it if you ask someone to do it for you?

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
13 years, 40 weeks ago
Page: << 1 >>
Topic is locked.
EOSERV Forum > Lounge > New to C++