?
Solved

Help with C function call

Posted on 2006-06-27
5
Medium Priority
?
292 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses

621 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