CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Newcomer to C

  1. #1
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    39

    Newcomer to C

    Hi, I am somewhat of a new programmer and the only experience I have was using Java. But anways, I am now learning to write a C program and am unsure of some of the new syntaxs and restrictions. I looked at a sample program that prints out the alphabet from A-Z.
    I am trying to understand this code, and when I rewrite it in my own words it works.
    One thing I am unsure of is the structure of my loop(s), I can get it to print out all the characters but I am trying to make the letters of the alphabet take up 3 lines rather than all on 1 line. Any suggestions are gratefully appreciated.
    Thanks in advance.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I don't really understand what your question is. If you're asking
    how to go to the next line, you use the newline character: '\n'.
    With this code:
    Code:
    #include <stdio.h>
    int main(int argc, char* argv[])
    {
       printf("line1\n");
       printf("this is on line2\n");
       printf("this is on line 3\n");
       return 0;
    }
    The following will be output:
    line1
    this is on line2
    this is on line 3
    --Paul

  3. #3
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    39
    Thanks for the reply. That kinda helped,
    but my output is getting printed out with a For Loop
    because my text is in an array? Any ideas..?
    Thanks again

  4. #4
    Join Date
    Apr 2003
    Location
    P.R.China
    Posts
    40
    Hi spinC ,
    Is this what you want?

    for(int i=0;i<ArrayCount;i++)
    {
    printf("%s\n",Array[i]);
    }

  5. #5
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    39
    That code printed out the same element of my array 2 times on the line. Not exactly..... I'll post my code to show what I have and what I am trying to do.

    int linenum=0;
    int count=0;
    char alpha[26] = "abcdefghijklmnopqrstuvxyz";

    for(i = 0;i <= strlen(alpha); i++)
    {
    printf("%c\n", alpha[i]);
    }

    return 0;

    This code prints out every letter on an individual line.
    I am trying to have a variable where I can print out the entire
    alphabet but I can specify how many lines to print it out on.

    ie. abcdef
    ghijklm
    nopqrs
    tuvwxy
    z

    5 lines

  6. #6
    Join Date
    Apr 2003
    Location
    P.R.China
    Posts
    40
    Hi spinC,

    I am trying to understand what you want, but your ie. puzzled me.

    ie. abcdef //6 characters
    ghijklm //7 characters ??
    nopqrs //6 characters
    tuvwxy //6 characters
    z //1 character

    If you want to print out 6 characters one line,hope this helps,

    int linenumber=0;
    int count=0;
    char alpha[26] = "abcdefghijklmnopqrstuvxyz";
    for(int i=0;i<strlen(alpha);i++)
    {
    printf("%c",alpha[i]);
    count++;
    if(count==6)
    {
    printf("\n");
    linenumber++;
    count=0;
    }
    }
    printf("\n\n");
    if(count!=0)
    {
    linenumber++;
    }
    printf("%d lines\n",linenumber);
    Last edited by Eric Leung; May 21st, 2003 at 09:30 PM.

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    This code example should print out the array in the specific no. of lines.

    Code:
    		char alpha[]="abcdefghijklmnopqrstuvwxyz";
    
    	const int lines = 5;
    	const int len = strlen(alpha);
    	
    	// Equivalent to len/lines and then round-up.
    	int charPerLine = (len + lines - 1)/lines; 
    
    	for(int i=0; i<len; ++i)
    	{
    		if(i && (i%charPerLine) == 0)
    			printf("\n");
    
    		printf("%c", alpha[i]);
    	}
    BTW, there is a mistake in the previous example,
    char alpha[26] = "abcdefghijklmnopqrstuvxyz";

    You need to allocate an extra space for '\0'.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  8. #8
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    39
    hmmmm.....I think what you posted might help but Im basically just trying to get it to print out the alphabet with a set number of characters per line, and this set number of characters per line is a number input by the user. Sorry I made a typo in the example so I see how it could be confusing. Thanks again for all the help though.

    ie. if they enter the value of 5

    abcde
    fghij
    klmno
    pqrst
    uvwxy
    z

  9. #9
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62
    try it:
    Code:
    void f(int s){
    	int i;
    	char alpha[]="abcdefghijklmnopqrstuvwxyz";
    	
    	for(i = 0;i <= strlen(alpha); i++)
    	{
    		if(!(i%s)) printf("\n");
    		printf("%c", alpha[i]);
    	}
    }
    
    void main(){
    	f(5);
    return;
    }

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by dav79
    try it:
    Code:
    void main(){
    }
    If they are using an ANSI C++ compiler, this won't compile. The main() function returns an int, not void.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2002
    Location
    Canada
    Posts
    39

    yup..

    thank you all for the replies and help....
    its good to go and prints out fine.
    thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured