Need help with array program.

This is a discussion on Need help with array program. within the C Programming forums, part of the General Programming Boards category; Here is what I need my output to look like. "What is your name? "Tom" Hello, Tom! Nice to meet ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    18

    Need help with array program.

    Here is what I need my output to look like.

    "What is your name? "Tom"
    Hello, Tom! Nice to meet you!
    The first character in your name is 'T'.
    Your name is 3 chars long.
    I changed the first character of your name to 'D'!
    Hello, Dom! Nice to meet you!
    The first character in your name is 'D'.
    Your name is 3 chars long.
    Page 1 of 4I chopped your name!
    Hello, D! Nice to meet you!
    The first character in your name is 'D'.
    Your name is 1 chars long."

    Here is my current code I need to use each of the functions listed at the top to receive full credit am I on the right track? Can anyone help me figure out what to do next?


    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void changeFirstChar(char* text, char first);
    
    void chopToFirst(char* text);
    
    void greet(char* name);
    
    char pickletter(void);
    
    
    int main(){
      char name[101];
    
      printf("What is your name?");
      scanf("%100s",name);
      greet(name);
    }
    
    void greet(char* name){
       printf("Hello %s! Nice to meet you!\n",name);
       printf("The first character in your name is %c\n",name[0]);
       printf("Your name is %d chars long\n",strlen(name));
    }
    
    void changeFirstChar(char* text, char first);
    
    char pickletter{
       char* letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       int letterIndex = rand() % strlen(letters);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It looks like you're on the right track. The next step, it seems would be to call pickletter() to get a random letter and then set the first letter of the name to that random letter. Then call greet() again with the changed name.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use fgets(name, sizeof(name), stdin); to get strings from the user, instead of scanf. Also, you need to call srand before you call rand. So include time.h and add this before the rand call.

    srand(time(NULL));
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Implement changeFirstChar. As a hint, you can modify letters in text like elements in an array. Remember too that a null character terminates a string in C.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your last function should return a char, and isn't returning anything atm.

    You'll want to remove the semi-colon at the end of this line of code:

    void changeFirstChar(char* text, char first);

    If you want to return a char, then you need a char variable to "catch" the return value and use it.


    You're on the right track(mostly), but you need to actually make and call the functions that you have prototyped at the top of your program. Don't worry about the details just now, work on the basics of your programs flow.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Thank all of you for the really fast replies. I seem to be having trouble with my pointers on the changeFirstChar function can anyone tell me how to declare char* text? I keep getting errors stating that it has already been declared. I can post what I have worked on if need be but I haven't got too far(I am very new at this)

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Do you have a local variable named text as well as a function parameter? Otherwise, we will probably need the updated version of your code.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program is set up to use char name[], rather than char text[] (or char *text). So replace "text" with "name", if you are going to use one char array.

    If you need to use 2 char arrays, then declare text[] in main().

    Yes, do post your latest code, because otherwise we're studying something obsolete.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    I don't think so... Ok but just a warning my code is in shambles(I am really new to this and what I have up there took several hours of trial and [mostly] failure).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void changeFirstChar(char* text, char first);
    
    void chopToFirst(char* text);
    
    void greet(char* name);
    
    char pickletter(void);
    
    
    int main(){
      char name[101];
    
      printf("What is your name?");
      scanf("%100s",name);
      greet(name);
    
    }
    
    void greet(char* name){
       printf("Hello %s! Nice to meet you!\n",name);
       printf("The first character in your name is %c\n",name[0]);
       printf("Your name is %d chars long\n",strlen(name));
    }
    
    void changeFirstChar(char* text, char first){
       text[0] = char first;
       pickletter(first);
       greet(first);
    }
    
    
    char pickletter(void){
       char* letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       int letterIndex = rand() % strlen(letters);
       //* How do I return the char here?*//
    }
    To adak. Thats what I was thinking but that function is what the teacher posted so I just thought it was another thing I didn't know(I've only been in the class for about two weeks....)
    Last edited by Mo777; 02-23-2011 at 04:00 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In main(), you need to now call your other functiions. To do that, you need to mimic what you did to call greet(), and you need to use name as the parameter, just like was done for greet().

    Use
    return letters[letterIndex]; //for your return.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Ok here is my compiler errors:

    person.c: In function ‚main‚:
    person.c:20: error: too few arguments to function ‚changeFirstChar‚
    person.c: In function ‚changeFirstChar‚:
    person.c:30: error: expected expression before ‚char‚
    person.c:31: error: too many arguments to function ‚pickletter‚
    person.c:33: warning: passing argument 1 of ‚greet‚ makes pointer from integer without a cast


    and code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void changeFirstChar(char* name, char first);
    
    void chopToFirst(char* text);
    
    void greet(char* name);
    
    char pickletter(void);
    
    
    int main(){
      char name[101];
    
      printf("What is your name?");
      scanf("%100s",name);
      greet(name);
      changeFirstChar(name);
    }
    
    void greet(char* name){
       printf("Hello %s! Nice to meet you!\n",name);
       printf("The first character in your name is %c\n",name[0]);
       printf("Your name is %d chars long\n",strlen(name));
    }
    
    void changeFirstChar(char* name, char first){
       name[0] = char first;
       pickletter(first);
       printf("I changed the first letter of your name to %c\n");
       greet(first);
    }
    
    
    char pickletter(void){
       char* letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       int letterIndex = rand() % strlen(letters);
       return letters[letterIndex];
    }
    How bad is it?

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    You need to call it like this:
    Code:
    changeFirstChar(name, pickletter());
    And declare it like this:
    Code:
    void changeFirstChar(char* name, char first)
    {
      name[0] = first;
      ...
    }
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    18
    Edit I fixed it but now I have a slightly different problem... The code compiles but my output looks like this

    "What is your name?Morgan
    Hello Morgan! Nice to meet you!
    The first character in your name is M
    Your name is 6 chars long
    I changed the first letter of your name to W
    Hello Norgan! Nice to meet you!
    The first character in your name is N
    Your name is 6 chars long"

    The W/N mix up is actually kind of funny but here is the code. * Also it seems to produce this output every time letting me think my random pickletter function failed.

    Code:
     GNU nano 1.3.12                                           File: person.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void changeFirstChar(char* name, char first);
    
    void chopToFirst(char* name);
    
    void greet(char* name);
    
    char pickletter(void);
    
    
    int main(){
      char name[101];
    
      printf("What is your name?");
      scanf("%100s",name);
      greet(name);
      changeFirstChar(name, pickletter());
      greet(name);
    }
    
    void greet(char* name){
       printf("Hello %s! Nice to meet you!\n",name);
       printf("The first character in your name is %c\n",name[0]);
       printf("Your name is %d chars long\n",strlen(name));
    }
    
    void changeFirstChar(char* name, char first){
       name[0] = first;
       first = pickletter();
       printf("I changed the first letter of your name to %c\n");
    }
    
    
    char pickletter(void){
       char* letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       int letterIndex = rand() % strlen(letters);
       return letters[letterIndex];
    }
    Last edited by Mo777; 02-23-2011 at 04:38 PM.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Code:
    void changeFirstChar(char* name, char first){
       name[0] = first;
       first = pickletter();
       printf("I changed the first letter of your name to %c\n");
    }
    You're already calling pickletter() and passing it as the second argument to the changeFirstChar() function. You don't need to call it again within the function, so you can totally get rid of the first = pickletter() statement.

    Your other problem is that you're not passing the %c substitution parameter to printf() so you're just seeing garbage. Try adding ', first' to the printf() call.
    If you understand what you're doing, you're not learning anything.

  15. #15
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Okay, the problem is in the printf in changeFirstChar. Change it to

    Code:
    printf("I changed the first letter of your name to %c\n", first);
    The %c in the printf statement tells it that it is to print a character, but you haven't told it which character to print. Leaving it out leads to strange behaviour, and I'm surprised your compiler compiled your code without complaining about it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21