Compiling trouble

hello, I have a code here.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main ()
{
  int i;
  cout << "Please enter the degrees in Celcius: ";
  cin >> i;
  cout << "That number in fahrenheit is: " << (i*1.8)+32 << ".\n";
  return 0;
}



It is very simple and other people say it works great. but when i compile it with DevC++ or visual C++ it will not work. in DevC++ when i compile it, it shows the first statement and then when you enter a number the console closes. and in visual C++ it does not even open for me.I am running Windows XP service pack 3. I am just confused why it is not working for me. I also installed DevC++ on another computer to see if it was just the computer but it did not work on that computer either.
Anything is that is helpful is welcome.

Thank you,
Davidbball13
Hell. almost all programs of that sort dont even work. they ALL close. (im back in DevC++ now
The program works like any other console program in windows.

It has already been explained here:
http://www.cplusplus.com/forum/beginner/1988/
hey. when i add.
1
2
3
4
5
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
  }


to the code it still shuts down when i enter a number. it made no change what so ever.
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main ()

{
    
  int i;
  cout << "Please enter the degrees in Celcius: ";
  cin >> i;
  cout << "That number in fahrenheit is: " << (i*1.8)+32 << ".\n";
  
  cin.ignore(1);
  cout << "Press ENTER to continue...";
  cin.get();

  return 0;
}
wow thanks so much it finally works. YES! now how could i have a option to do another conversion or to exit. like a "Would you like to do another conversion Y or N)
create a boolvariable and use a do/while loop with as condition that variable, and set the variable (inside the loop) to false if the user inputs N
Thanks, but now that i think of it the Y or N would not be very help full. plus the program is half a meg and takes now processing power to open it so why not just double click it. Iv always wondered how to do this here is my code at the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main ()
{
    system("TITLE C2F");
    system("COLOR 2");
    system("PAUSE");
    system("cls");
int i;
  cout << "Please enter the degrees in Celcius: ";
  cin >> i;
  cout << "That number in fahrenheit is: " << (i*1.8)+32 << ".\n";
    cin.ignore(1);
  cout << "Press ENTER to exit...";
  cin.get();
  return 0;
}


So after you hit enter it closes. how can i make it have another cout saying "goodbye" and have that ere for about like .75 seconds. and then have it close. so you hit enter and then the screen clears and then the message " goodbye" comes up for a little under a second.
You would have to pause the program a short time. In order to do that, you have to include <time.h> and use the function clock(). See http://www.cplusplus.com/reference/clibrary/ctime/clock.html
(you have do define you own function, but in that function you will need clock())
Last edited on
Ill pull apart that code later. thanks a lot.
It's not as hard as it may look. There is a ready-to-use example in the reference page:

1
2
3
4
5
6
void wait ( int time )
{
  clock_t endwait;
  endwait = clock () + time;
  while (clock() < endwait) {}
}


clock_t is a variable-type to store a value in thats returned by clock(). When you call this function, it first looks on wich time to pause should end:endwait = clock () + time; and then gets into a loop until that time is reached: while (clock() < endwait) {}.

It is good to understand everyting your using in your code, but you could cut/paste the above code to your program and call it as wait(750), and look at how it works later ;)
Last edited on
Yeah I am just confused were to put all of it. so i think i would be better off knowing how to use it first then do that. i just dont know were to put those in.
because my current code is (i did some additions to it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main ()
{
    system("TITLE C2F");
    system("COLOR 2");
    system("PAUSE");
    system("cls");
int i;
  cout << "Please enter the degrees in Celcius: ";
  cin >> i;
  cout << "That number in fahrenheit is: " << (i*1.8)+32 << ".\n";
        cin.ignore(1);
      cout << "Press ENTER to exit...";
  cin.get();
  return 0;
}


i know that i would have to add the #include <time.h>
but im stuck there. thanks for you time in advance.
Last edited on
You do know how to define another function as main, right?
I know iv done it before i just cant rember how.
http://www.cplusplus.com/doc/tutorial/functions.html

Its sort of the base concept of c++. If you're new to programming i would recommend to read the whole tutorial.

This is your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <time.h>      //include time.h

using namespace std;

void wait ( int time )           //define function wait() with the parameter time of type int0
{
  clock_t endwait;
  endwait = clock () + time;     //clock() returns the time the program has run in seconds*1000:
                                 //1000 is 1 second, 750 is 0.75 second
  while (clock() < endwait) {}
}

int main ()
{
    system("TITLE C2F");
    system("COLOR 2");
    system("PAUSE");
    system("cls");
int i;
  cout << "Please enter the degrees in Celcius: ";
  cin >> i;
  cout << "That number in fahrenheit is: " << (i*1.8)+32 << ".\n";
        cin.ignore(1);
      cout << "Press ENTER to exit...";
  wait (750);                 //call the function wait with as argument 750
  return 0;
}


This may be a littlebit to far ahead, i would first try to get the basics down (read the tutorial).
Last edited on
The wait thing is very close to PIC BASIC micro controllers. I already have an idea what i can do with the timing command. thanks a lot.
just out of curiosity, could you not just include 'winbase.h' and use Sleep() to suspend the thread for the timeout?
I guess. I'm not yet really familiair with the windows librarys, but i have tested it, and you can use it to pause the program to, yes (you have to include windows.h to).

However, i prefere the clock() function in time.h: it gives more options. For example, you could use it to keep doing a certain action for a certain time:
1
2
3
4
5
6
7
//displays the time in seconds during one minute
clock_t stop=clock()+60*1000;
while (clock()<stop)
{
system("cls");
cout<<(clock()/1000);
}


You can do this also using sleep(), but i guess you got the point ;)
Last edited on
Topic archived. No new replies allowed.