Simple C Program
Hello.
I'm reading C Primer Plus while trying to learn the C programming language. I've only been at it for about a week. I'm having trouble figuring out how to create a simple converter. I'm trying to make the program start by telling the user to enter the amount of fathoms he wishes to convert into feet. After the user enters a number, I want the program to respond with something like, "There are __ feet in __ fathoms." There are probably many ways of doing this, but I want to know if it's possible using only the functions I've been practicing. If you would be so kind, please give me a list of possible C source codes for such a program. Try to make it as similar to my own as possible, except make sure yours works. =)
Here's a paste of my flawed source code:
http://rafb.net/paste/results/As660589.html
Thanks, again.
[edited by - SirDeity on October 11, 2003 10:36:27 PM]
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char *argv[] ){
int fathoms;
printf("Enter the number of fathoms you'd like to convert into feet.\n");
scanf("%d", &fathoms);
printf("There are %d feet in %d fathoms!\n", fathoms*6, fathoms);
return 0;
}
sorry if the C sintax isnt very good, long time being using C++ only.
the problem with your function is that your tried to multiply by 6 before you had the fathoms.
int x = x*6; //very wrong
hope it helps.
[edited by - a2ps on October 11, 2003 10:51:09 PM]
#include <stdio.h>
int main( int argc, char *argv[] ){
int fathoms;
printf("Enter the number of fathoms you'd like to convert into feet.\n");
scanf("%d", &fathoms);
printf("There are %d feet in %d fathoms!\n", fathoms*6, fathoms);
return 0;
}
sorry if the C sintax isnt very good, long time being using C++ only.
the problem with your function is that your tried to multiply by 6 before you had the fathoms.
int x = x*6; //very wrong
hope it helps.
[edited by - a2ps on October 11, 2003 10:51:09 PM]
#include <stdio.h>
#include <stdlib.h>
main(void)
{
int feet, fathoms;
printf("Enter the number of fathoms you''d like to convert into feet.\n");
scanf("%d", &fathoms); // You must first read the amount of fathoms
feet = fathoms * 6; //Then you do the conversion
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
getchar();
getchar();
return 0;
}
You had an error in line 8 "fathoms = fathoms*feet;" because you were multiplying a variable, fathoms, with no inicial value.
#include <stdlib.h>
main(void)
{
int feet, fathoms;
printf("Enter the number of fathoms you''d like to convert into feet.\n");
scanf("%d", &fathoms); // You must first read the amount of fathoms
feet = fathoms * 6; //Then you do the conversion
printf("There are %d feet in %d fathoms!\n", feet, fathoms);
getchar();
getchar();
return 0;
}
You had an error in line 8 "fathoms = fathoms*feet;" because you were multiplying a variable, fathoms, with no inicial value.
Move the fathoms=fathmos*feet line below the scanf line - (and before the printing results line) should work then...
Those were some fast respones. Thank you all!
Dr_BCC, perfect response. It was better than I had hoped for. Thank you very much! Now I completely understand my mistake.
Dr_BCC, perfect response. It was better than I had hoped for. Thank you very much! Now I completely understand my mistake.
I''m having another similar problem. I''ve been trying to make this work for over two hours. The book hasn''t covered assigning words or sentences to variables. I only know how to assign numbers. Using the same code, or using as little new code as possible, how can you make this work?
Assignment: Write a program that produces the following output:
For he''s a jolly good fellow!
For he''s a jolly good fellow!
For he''s a jolly good fellow!
Which nobody can deny!
Instructions: Have the program use two user-defined functions in addition to main(): one that prints the jolly good message once, and one that prints the final line once.
This time, I don''t think I''m anywhere near being close. The best response would be one similar to Dr_BCC''s previous one. Here''s a paste of my flawed source code. I''ll include a couple different failed attempts so you can guess what my intentions were.
http://rafb.net/paste/results/x2992340.html
Assignment: Write a program that produces the following output:
For he''s a jolly good fellow!
For he''s a jolly good fellow!
For he''s a jolly good fellow!
Which nobody can deny!
Instructions: Have the program use two user-defined functions in addition to main(): one that prints the jolly good message once, and one that prints the final line once.
This time, I don''t think I''m anywhere near being close. The best response would be one similar to Dr_BCC''s previous one. Here''s a paste of my flawed source code. I''ll include a couple different failed attempts so you can guess what my intentions were.
http://rafb.net/paste/results/x2992340.html
Just the relevant portions of the code:
void Jolly ( void )
{
printf ( "For he''s a jolly good fellow,\n" );
}
void Deny ( void )
{
printf ( "Which nobody can deny.\n" );
}
int main ( void )
{
for ( int a = 0; a < 3; a++ )
Jolly ();
Deny ();
return 0;
}
void Jolly ( void )
{
printf ( "For he''s a jolly good fellow,\n" );
}
void Deny ( void )
{
printf ( "Which nobody can deny.\n" );
}
int main ( void )
{
for ( int a = 0; a < 3; a++ )
Jolly ();
Deny ();
return 0;
}
That''s C I haven''t learned yet; the book hasn''t covered it. Are there any other ways of creating this program?
These lines haven''t been discussed in the book yet:
for ( int a = 0; a < 3; a++ )
// I haven''t yet learned how to use the "for" keyword.
Jolly ();
// I haven''t learned how to use the void word(void) functions yet. The only voids used so far are main and butler. I haven''t seen any examples of the word(); function being used.
Deny ();
// Same as the above statement/function.
Thanks! =)
These lines haven''t been discussed in the book yet:
for ( int a = 0; a < 3; a++ )
// I haven''t yet learned how to use the "for" keyword.
Jolly ();
// I haven''t learned how to use the void word(void) functions yet. The only voids used so far are main and butler. I haven''t seen any examples of the word(); function being used.
Deny ();
// Same as the above statement/function.
Thanks! =)
Without knowing the for loop, you could just do
Jolly();
Jolly();
Jolly();
------------------------------------------------------------
// TODO: Insert clever comment here.
Jolly();
Jolly();
Jolly();
------------------------------------------------------------
// TODO: Insert clever comment here.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement