Solved

Help with C function call

Posted on 2006-06-27
5
253 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 250 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 250 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

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

943 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now