Archived

This topic is now archived and is closed to further replies.

SirDeity

Simple C Program

Recommended Posts

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]

Share this post


Link to post
Share on other sites
#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]

Share this post


Link to post
Share on other sites
#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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hehe, 3 reply''s and all telling the same.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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! =)

Share this post


Link to post
Share on other sites
Without knowing the for loop, you could just do

Jolly();
Jolly();
Jolly();





------------------------------------------------------------
// TODO: Insert clever comment here.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You don''t have to, normally (unless the compiler is really picky...)

no keyword defaults to ''void'' .. that is... returning no value.

Share this post


Link to post
Share on other sites
New Question: Assignment #7

My program works fine, but I don''t think I followed the instructions correctly. Please look at my source code and tell me if I followed the instructions correctly. If I didn''t, please give hints and/or examples on how to find the same answer using the correct method.

Instructions (from book): Write a program that calls a function named one_three(). This function should display the word "one" on one line, call the function of two(), and then display the word three on one line. The function two() should display the word two on one line. The main() function should display the phrase starting now: before calling one_three() and display done! after calling it. Thus, the output should look like the following:

starting now:
one
two
three
done!


Here is my pasted source code:

http://rafb.net/paste/results/R2189478.html


Thanks! =)

Share this post


Link to post
Share on other sites
Almost, but you forgot to make the two() function.

Why exactly are you doing exercises on material you haven't read about yet? The previous one specifically asks for user-defined functions, which you say you haven't covered. =\

[edited by - twix on October 12, 2003 4:38:51 PM]

Share this post


Link to post
Share on other sites
twix, the assignments I''m doing are for the chapter I''ve thoroughly read. I don''t think they covered the user-defined functions very well, but they apparently were covered. It could be just me, but it doesn''t matter anymore because thanks to all the responses I''ve received, I know understand it.

Can you show me how to make the two() function? I''m not sure how they want me to make one_three() call for two().

Share this post


Link to post
Share on other sites
I think I understand what they want. Is this the correct source code?

http://rafb.net/paste/results/Y2992378.html

[edited by - SirDeity on October 12, 2003 5:38:12 PM]

Share this post


Link to post
Share on other sites
That seems right. Nevertheless, I think you should read the chapters much more carefully before you attempt the exercises.

Share this post


Link to post
Share on other sites