Thread: Can someone help

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    Can someone help

    I wonder if someone can help me out. I am trying to do this project for school and I don't really understand this to much. I am trying to get the user to enter 2 words and a number. Then capatilize the words and tell the user if either word contains the same number of letters as the number they entered. This is what I have come up with so far.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h>
    #include <ctype.h>
    
    #define MAXSTRING 100
    
     
    
    int main(void)
    
    char w, word[MAXSTRING];
    int i;
    
    {
    	printf("Please enter 2 words\n");
    
    	for (i=0;(w = getchar()) != '\n'; ++i) {
    		word[i]=w;
    		if (isalpha(w))
    			sum +=w;
    	}
    
     
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>2 words
    Use 2 sperate calls to fgets(), or use 1 call to get both words, and split the data in memory.
    Suggested reading

    >>1 number
    Use fgets() again, then convert the string to a number.
    More suggested reading
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Thanks for the links but I can't really figure out what I am doing. Is this anything that is even kind of close.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    
    int main()
    {
    	char WORDS[2][50];
    	int i;
       
      
      printf ("Please enter 2 words\n");
        for(a=0; a < 2; a++) 
      {
        printf("Enter a word: "); /*get the words*/
        fgets(WORDS[i], 50, stdin);
    }

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You're very close

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    
    int main()
    {
      char WORDS[2][50];
      int i;
       
      for(i=0; i < 2; i++)
      {
        printf("Enter word %i: ", i+1); /*get the words*/
        fgets(WORDS[i], 50, stdin);
      }
    
      return 0;
     }
    The other stuff is fairly easy too as you'll discover.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, what are you trying to accomplish with 'sum+=w'? You are adding the value of the char, not simply counting it. Change to '++sum', and you will get the count (be sure to initialize sum to 0 of course...). For capitalization, just use a simple flag to tell you if you've hit the beginning of a word:

    int ready = TRUE;

    if(isalpha(w))
    {
    if(ready)
    {
    ready = FALSE;
    w = toupper(w);
    }
    }
    else
    {
    ready = TRUE;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I still really don't understand this. I tried some of the things you have recomended but it still doesn't work.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    
    int main()
    {
      char WORDS[2][50];
      int i;
       
      for(i=0; i < 2; i++)
      {
        printf("Enter word %i: ", i+1); /*get the words*/
        fgets(WORDS[i], 50, stdin);
      }
    
      printf("\nPlease enter a number\n");
      scanf("%d",&i);
    
      return 0;
     }
    
    
    int ready = TRUE;
    
    if(isalpha(w))
    {
    if(ready)
    {
    ready = FALSE;
    w = toupper(w);
    }
    }
    else
    {
    ready = TRUE;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @thatdude: The first part of your code looks OK. Now to upper case everything, simply loop through the array, calling toupper() on each element.

    Heres an example of how to do this, you'll need to adapt this code to work with yours though.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(void)
    {
      char  myword[] = "testing";
      int   i, len;
      
      puts (myword);
      
      len = strlen(myword);
    
      for (i = 0; i < len; i++)
      {
        myword[i] = toupper(myword[i]);
      }
    
      puts (myword);
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    thanks for the information but im still lost.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, I would love to help, but from what I've seen of how you are applying the advice of others, you simply don't have a grasp of the basics. For example, the snippet I gave you didn't even end up in main() ! How could it possibly work then?! You have to go step by step, and get a firm grip on each aspect of coding before trying anything new. If this is a homework assigment, why not just tell your professor that you still feel a little lost and need some more help? But above all, read your textbooks front to back until everything is perfectly clear. I would suggest to you 'C: A Software Engineering Approach' by Darnell and Margolis, an excellent beginners book. Anyway, even if we solved this for you, you wouldn't learn a thing from it. You must do it yourself, and have the determination to understand the problem well before attempting to solve it.

    - cheers.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    OK I made more changes to this. Am I even getting warm.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    
    int main(int argc, char * argv[]) 
    {
    	char WORDS[2][50];
    	int i;
    	int ready = TRUE;
       
      
        printf ("Please enter 2 words\n");
        for(a=0; a < 2; a++) 
    
    	{
        printf("Enter a word: "); /*get the words*/
        fgets(WORDS[i], 50, stdin);
    	}
    
    	printf("\nPlease enter a number\n");
    	scanf("%d",&i);
    
    	if(isalpha(w))
    		{
    		if(ready)
    			{
    			ready = FALSE;
    			w = toupper(w);
    			}
    		}
    	else
    		{
    		ready = TRUE;
    		}
    
    
    	return main(argc,argv);
    
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Cold as ice.

    Hint: for one, leave out the code from my signature.

    Second of all, the snippet I gave you belongs in a loop, ie: loop thru each element in the array.

    Again, I really think you need to hit those textbooks a little harder.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    thanks for your posts. i have been doing good in this class but the whole book only has one chapter on strings.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    55
    OK I have been playing around with this more and I have it working with the exception of capitalizing the letters. I have discovered the book we use for thsi class isn't that great. Is there a place on teh web I can read up on how to capitalize letter? Can someone help me out a little with capitalizing letters?

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    
    int main()
    {
        char first[80], second[80]; 
    	int lengthF=0, lengthS=0;
    	int i=0;
    
        printf("\nEnter a word: ");
        gets(first);
        printf("\nEnter another word: ");
        gets(second);
    
    	printf("\nNow enter a number:");
    	scanf("%d", &i);
    
    	lengthF = strlen(first);
    	lengthS = strlen(second);
    
    	if (lengthF == i)
            printf("\n%s has %d letters in it", first, i);
    
    	if (lengthS == i)
            printf("\n%s has %d letters in it", second, i);
    
        return 0;
    }

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    How to capitalize a letter or a string?
    To capitalize a string use the toupper() function, you can find it in string.h

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I tried it but I must be doing something wrong.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <ctype.h>
    
    
    int main()
    {
        char first[80], second[80]; 
    	int lengthF, lengthS,i=0;
    	
    
        printf("\nEnter a word: ");
        gets(first);
        printf("\nEnter another word: ");
        gets(second);
    
    	printf("\nNow enter a number:");
    	scanf("%d", &i);
    
    	lengthF = strlen(first);
    	lengthS = strlen(second);
    
    
    
    	first[lengthF]= toupper(first[lengthF]);
    	second[lengthS]= toupper(second[lengthS]);
    
    
    
    	if (strcmp(first, second) == 0)
            puts("You entered the same words");
        else
            puts("The two words you entered were not the same");
    
    
    	if (lengthF == i)
            printf("\n%s has %d letters in it", first, i);
    	else
    		printf("\nThe first word you entered does not have the same number of letters as the number you entered.");
    
    	if (lengthS == i)
            printf("\n%s has %d letters in it", second, i);
    	else
    		printf("\nThe second word you entered does not have the same number of letters as the number you entered.");
    
    	getchar();
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed