GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jan-2006, 19:43
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

A program to convert miles to kilometres


Im a noob trying to learn C by making simple programs as i go and need help, i cant get a program to convert miles to kms that i made to work because it asks for a number of miles to convert, but then i dont know how to get it to actually convert and another one real simple, just opens a box and prints out Hello, but for some reason that box lasts less then a second, how can i get these to work out? (the mile-km converter closes wen i press enter)
  #2  
Old 29-Jan-2006, 19:51
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: newbie needing help for C


Hi Persian,

Welcome to the GIDForums.

Which compiler do you use?

Do you use an IDE?
If so, you must pause the program for a while, so that you can see the output.
Please take a look here:
Pausing a program

Please read the Guidelines too.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 29-Jan-2006, 19:58
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

Re: newbie needing help for C


I just looked thru the link u gave, and i tried the 3 different options it gives u, finding getchar();the best but now the window opens and no words come up?

this is the code

CPP / C++ / C Code:
 #include <stdio.h>
 int
 main(void)
 {
  getchar();
  printf("Hello\n");
  return (0);
 }
Last edited by LuciWiz : 30-Jan-2006 at 09:01. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 29-Jan-2006, 20:00
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

Re: newbie needing help for C


"P wait the guidlines say to do this so

CPP / C++ / C Code:
 #include <stdio.h>
 int
 main(void)
 {
  getchar();
  printf("Hello\n");
  return (0);
 }
Last edited by LuciWiz : 30-Jan-2006 at 09:03. Reason: You need to use an end tag too ([/c])
  #5  
Old 29-Jan-2006, 20:07
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: newbie needing help for C


Put the getchar before return 0;
CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
    printf("Hello\n");
    
    getchar();
    return 0;
} 
Because you have to wait before quitting the program.
Always put your pause functions before return 0;

Insert your code between [c] and [/c] tags.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 29-Jan-2006, 20:15
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

Re: newbie needing help for C


thx, it works now ,
btw with these commands, is there a method of know wat order commands like that go in, like where to put them?
  #7  
Old 29-Jan-2006, 20:23
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: newbie needing help for C


Quote:
Originally Posted by Persian
btw with these commands, is there a method of know wat order commands like that go in, like where to put them?
According to the need!

If you want to pause the program before the end of the program, put it before return 0;

For example, consider this code:
CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
    printf("Hello ");
    getchar();

    printf("World");
    getchar();
    return 0;
} 
Did you understand how this works?

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 29-Jan-2006, 20:24
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

Re: newbie needing help for C


kk, thx alot
  #9  
Old 29-Jan-2006, 20:39
Persian_Devil13 Persian_Devil13 is offline
New Member
 
Join Date: Jan 2006
Posts: 11
Persian_Devil13 is on a distinguished road

Re: newbie needing help for C


U woodnt happen to know what to do about a kilometre-mile converter that asks for a number in miles but closes wen u give it a number and press enter, i dont get how it to work so that it actualy converts
  #10  
Old 29-Jan-2006, 20:54
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: newbie needing help for C


You would've done an illegal operation.
For example, if you give a character as input when you need integer, the program would quit.
If you divide a number by 0, the program would quit.

Please post your code, so that we'll be able to find out what is the error.

There is also another source for error.
The scanf function!

If you use scanf, it will lead to a lot of errors. It will put the extra characters in the stdin buffer, and one getchar wont work.
You can look at the C/C++ tutorial subforum to find a lot of tutorial on scanf.

When you use scanf, put getchar after that scanf, and this will work.
There are also other methods.

For example, like this:
CPP / C++ / C Code:
#include <stdio.h>
int main(void)
{
    float km, miles;
    scanf("%f", &km);
    getchar();

    miles = 1.609344 * km;
    printf("km = %f miles = %f", km, miles);
    getchar();
    return 0;
} 

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C program to convert NFA to DFA ayoub C Programming Language 11 14-Sep-2007 14:51
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 12:52
Is my program coded well? dave88 C Programming Language 6 14-Oct-2005 09:18
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:02.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.