Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with C function call

Posted on 2006-06-27
5
Medium Priority
?
291 Views
Last Modified: 2010-04-15
I have a quick question while trying to learn C programming from Sams Teach Yourself C Programming in 21 Days.

I'm trying to call a straightforward print function to main, and have the function print data in a certain pattern. I need a little guidance on how to print the following pattern:

Stop!Stop!Stop!
Stop!Stop!
Stop!

I understand how to do the function prototyping OK. I have defined the function as:

void stop(void)
{
     printf("Stop!");
}

Now, is the best approach to use a \n somewhere in the function call, like this:

int main(void)
{
      stop(), stop(), stop(); \n
      stop(), stop(); \n
      stop(); \n

Or to simply put the \n in the function definition, like this:

void stop(void)
{
     printf("Stop\n");
}

If the above is right (which I think is the case), how do I call this function to produce the pattern above?

Please respond as soon as possible.

Thanks for your help.
0
Comment
Question by:computerese
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16995827
Hi computerese,

The best method here would be to use a loop. Two 'for' loops would do. The outer one will go from 3 downwards (how many 'Stop's per line) and another one INSIDE that one printing that many 'Stop's. Why dont you have a go at that and post code if you get stuck.

I'm trying to give you help rather than answers here as this would be classified as 'homework' here at EE.

Paul
0
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 1000 total points
ID: 16996593
and if you have not yet learned the for loop, you can also define a newline function like this:

void newline(void)
{
     printf("\n");
}

and,

> stop(), stop(), stop(); \n

there is no reason to use the comma operator here, you can simply use the semicolon.
0
 

Author Comment

by:computerese
ID: 16996981
To: cwwkie:

Thanks for your comments. I had been trying to use the \n at the end of the statements, as you suggest and as can be seen from the example I included with my question. But I was, and still am, getting the following error: stray "\" in program. Maybe I should have simply asked what would cause this error? Could you help me with that? Your other suggestion about creating a newline function worked great! But I am in the learning stage, obviously (and I am teaching this stuff to myself with just the book I mentioned--not in any class), so many subtleties of C syntax escape me and would really like enlightenment on why I'm getting this error.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 16997074
>>      stop(), stop(), stop(); \n
You need somethink like:

      stop(); stop(); stop(); printf("\n");

Paul
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17004919
@computerese

> Maybe I should have simply asked what would cause this error?

I think that would have been better. And we do not know how much you have learned from c. So if we suggest something which you have not learned yet, just say so, there is always an alternative way to solve a problem.

The reason why you got an error on the \n, is because \n is not a valid statement. The easiest thing to remember for now is, you always have to do "something'. That can be calling a function, or assign a variable.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question