Doesn't consider change in variable

This is a discussion on Doesn't consider change in variable within the C Programming forums, part of the General Programming Boards category; First of all, hi guys, new here, just started college in networks and systems, I'm completely new to programming and ...

  1. #1
    Registered User f0xy's Avatar
    Join Date
    Oct 2011
    Posts
    3

    Doesn't consider change in variable

    First of all, hi guys, new here, just started college in networks and systems, I'm completely new to programming and C is the language we're working in, so sorry if this actually a really dumb problem.

    Righto, onto the issue itself.
    I'm supposed to write something that depending on two variables, will repeat hello in a different language, n being the number of times it repeats, and l the language it's in. Thing is, the value of l doesn't seem to matter, as it always prints the first language.
    Using gcc shows no errors in code, and I can't figure out what's wrong.

    Code:
    #include <stdio.h>
    int main() {
    int n,x,l;
        scanf ("%d\n %d",&n,&l);
        x=1;
        n<=100;
        l>=1 && l<=5;
        for (l=1; x <= n; x++)    
                printf ("GOOD LUCK\n");
        for (l=2; x <= n; x++)        
                printf ("BUENA SUERTE\n");
        for (l=3;x <= n;x++)            
                printf ("BONNE CHANCE\n");
        for (l=4;x <= n;x++)            
                printf ("BUONA FORTUNA\n");
        for (l=5;x <= n;x++)            
                printf ("VIEL GLUECK\n");
    return 0;
    }
    Could you guys point me in the right direction ?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Well lines 6 and 7 don't do anything. They evaluate a boolean expression, then throw the answer away.

    Consider some more meaningful variable names - does it look like it makes sense to you?
    Code:
    #include <stdio.h>
    int main() {
    int numberOfTimesToPrint,x,whichLanguageToPrint;
        scanf ("%d\n %d",&numberOfTimesToPrint,&whichLanguageToPrint);
        x=1;
        numberOfTimesToPrint<=100;
        whichLanguageToPrint>=1 && whichLanguageToPrint<=5;
        for (whichLanguageToPrint=1; x <= numberOfTimesToPrint; x++)
                printf ("GOOD LUCK\n");
        for (whichLanguageToPrint=2; x <= numberOfTimesToPrint; x++)
                printf ("BUENA SUERTE\n");
        for (whichLanguageToPrint=3;x <= numberOfTimesToPrint;x++)
                printf ("BONNE CHANCE\n");
        for (whichLanguageToPrint=4;x <= numberOfTimesToPrint;x++)
                printf ("BUONA FORTUNA\n");
        for (whichLanguageToPrint=5;x <= numberOfTimesToPrint;x++)
                printf ("VIEL GLUECK\n");
    return 0;
    }
    Consider a basic idea such as
    Code:
    if ( whichLanguageToPrint == 1 ) {
      for ( x = 1 ; x <= numberOfTimesToPrint; x++)
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    The statements
    Code:
    n<=100;
    l>=1 && l<=5;
    effectively do nothing. You have to use an if statement, like this:
    Code:
    if(n <= 100 && (l >= 1 && l <= 5))
    {
        ...
    }
    You seem to use the for loop incorrectly. It is used like this:
    Code:
    for(initialization; condition; counting)
    {
        ...
    }
    As in
    Code:
    for(start = 0; start < end; ++start)
    {
        ...
    }
    A switch statement could help:
    Code:
    switch(l)
    {
        case 1:
            for (x = 1; x <= n; x++)
                printf ("GOOD LUCK\n");
        break;
        case 2:
            for (x = 1; x <= n; x++)
                printf ("BUENA SUERTE\n");
        break;
        case 3:
            for (x = 1; x <= n; x++)
                printf ("BONNE CHANCE\n");
        break;
        case 4:
            for (x = 1; x <= n; x++)
            printf ("BUONA FORTUNA\n");
        break;
        case 5:
            for (x = 1; x <= n; x++)
                printf ("VIEL GLUECK\n");
        break;
    }
    But that is painfully ineffective. Instead, you should use an array.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        char *greets[] = { "GOOD LUCK",
                           "BUENA SUERTE",
                           "BONNE CHANCE",
                           "BUONA FORTUNA",
                           "VIEL GLUECK" }
    
        ...
    
        for(x = 0; x < n; ++x)
            printf("%s\n", greets[l]);
    
        return EXIT_SUCCESS;
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  4. #4
    Registered User f0xy's Avatar
    Join Date
    Oct 2011
    Posts
    3
    Thanks for the help guys, I've managed to fix it according to your guidance.

    About lines 6 and 7, it's something I forgot to mention in the OP, I'm asked to limit the input to those numbers, although that might not be the best way, as I'll change it to how Hauzer said.
    @Hauzer, I haven't approached the case/break/switch part yet, although if I understand correctly, that's effectively defining every possible case ?
    Again, thanks for pointing me in the right way guys, it's running perfectly now

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Quote Originally Posted by f0xy View Post
    @Hauzer, I haven't approached the case/break/switch part yet, although if I understand correctly, that's effectively defining every possible case ?
    Yes.
    Code:
    switch(number)
    {
        case 1:
        {
            printf("This is number one.\n");
        }
        break;
        case 4:
        {
            printf("This is number four.\n");
        }
        break;
        case 153:
        {
            printf("This is number one hundred fifty three.\n");
        }
        break;
        default:
            printf("This is an unknown number.");
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    Registered User f0xy's Avatar
    Join Date
    Oct 2011
    Posts
    3
    I see, so every case that's defined will depend on the variable matching it, if none match then it defaults back to a defined answer. Although it makes sense I'll look it up further, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't change variable from another function...
    By Ryan. in forum C Programming
    Replies: 9
    Last Post: 03-10-2011, 11:10 PM
  2. Replies: 4
    Last Post: 02-28-2011, 06:57 AM
  3. change variable and repeat
    By altf4thc in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2010, 04:27 PM
  4. Replies: 2
    Last Post: 12-16-2006, 05:53 PM
  5. unwanted Variable Change
    By purebuu in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 07:56 PM

Tags for this Thread


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