#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Avoiding alphabets...any SUGGESTIONS???


    i tried using scanf but its giving me a hard time..i just want my program to loop if user entered an invalid key. i.e. input<0, input>limit and ALPHABETS,,,it converts it and skips to the next statement

    here's my code

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<iostream>
    #include<ctype.h>
    #define br 800
    #define sqlot 500
    #define sqfl 600
    #define lot1 1000
    #define lot2 1500
    #define lot3 2000
    #define lot4 2500
    #define lot5 3000
    main()
    {
          char count;
        for(count=0;count=10;count++) 
        {
            char opt,l[1],f[1],la[1],fa[1],bed1[1],bed2[1],bed3[1],tb[1],totexp[1];
            int L,F,LA,FA,Bed1,Bed2,Bed3,TB,TotExp;
            system("cls");
            printf("\n\n\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +   This program will calculate the expense of building a house.\t   +");
            printf("\n   +   \t\tAlways verify choice before proceeding.\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            getch();
            system("cls");
            printf("\n\n\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +   Input the required input then press[ENTER] key...\t           +");
            printf("\n   +   Pressing an incorrect input will close this program.\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            getch();
            system("cls");
                      do
                      {
                                               printf("\nPlease select desired lot by pressing the corresponding number");
                                               printf("\n[1]Sampaguita Drive");
                                               printf("\n[2]Mahogany Boulevard");
                                               printf("\n[3]Diamond Street");
                                               printf("\n[4]Moon Valley");
                                               printf("\n[5]Heaven's Gate:\t");
                                               scanf(" %d",&L);
                                    switch(L)
                                             {
                                               case 1:
                                               printf("\nYou have selected:\tSampaguita Drive %d per square meter",lot1);break;
                                               case 2:
                                               printf("\nYou have selected:\tMahogany Boulevard %d per square meter",lot2);break;
                                               case 3:
                                               printf("\nYou have selected:\tDiamond Street %d per square meter",lot3);;break;
                                               case 4:
                                               printf("\nYou have selected:\tMoon Valley %d per square meter",lot4);break;
                                               case 5:
                                               printf("\nYou have selected:\tHeaven's Gate %d per square meter",lot5);break;
                                               default:
                                               printf("\nInvalid choice");break;
                                             }                                         
                                    printf("\nAre you sure?");
                                    printf("\nEnter [n] to go back, [y] to proceed:\t");
                                    scanf(" %c",&opt);
                      }
                         while((opt=='n')||(opt=='N')||(!((L>0)&&(L<=5))));
                         system("cls");
                      do
                      {
                                      printf("\nNow enter desired lot area:\t");
                                      printf("\nMAXIMUM OF 500 square meters\t");
                                      scanf(" %d",&LA);
                                      printf("\nYou have entered %d square meters",LA);
                                      printf("\nAre you sure?");
                                      printf("\nEnter [n] to go back, [y] to proceed:\t");
                                      scanf(" %c",&opt);
                      }
                          while((opt=='n')||(opt=='N')||(LA<0)||(L>=500));
                          system("cls");
                    do
                    {                             
                                      printf("\nNow enter desired floor area:\t");
                                      scanf(" %d",&FA);
                                      printf("\nYou have selected %d square meters",FA);
                                      printf("\nAre you sure?");
                                      printf("\nEnter [n] to go back, [y] to proceed:\t");
                                      scanf(" %c",&opt);
                    }
                    while((opt=='n')||(opt=='N')||(!((FA>0)&&(FA<=500))));
                    system("cls");
                do
                {
                              printf("\nMAXIMUN OF 3 FLOORS ONLY");
                              printf("\nNow enter how many floors you wish your house will be:\t");
                              scanf(" %d",&F); 
                              printf("\nYou have selected %d floors",F);
                              printf("\nAre you sure?");
                              printf("\nEnter [n] to go back, [y] to proceed:\t");
                              scanf(" %c",&opt);
                              
                }
                while((opt=='n')||(opt=='N')||(!((F>0)&&(F<=500))));
                system("cls");    
        do
        {
        switch(F)
                 {
                      case 1:
                              printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                              printf("\nNow enter how many bedrooms you wish your house will be:\t");
                              scanf(" %d",&Bed1);
                              TB=(Bed1);
                      printf("\nYou have selected %d bedroom(s)",TB);
                      printf("\nAre you sure?");
                      printf("\nEnter [n] to go back, [y] to proceed:\t");
                      scanf(" %c",&opt);break;
                      case 2:
                             printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                             printf("\nNow enter how many bedrooms you wish your house will be on the first floor:\t");
                             scanf(" %d",&Bed1);
                             printf("\nNow enter how many bedrooms you wish your house will be on the second floor:\t");\
                             scanf(" %d",&Bed2);
                             TB=((Bed1)+(Bed2));
                      printf("\nYou have selected %d bedroom(s)",TB);
                      printf("\nAre you sure?");
                      printf("\nEnter [n] to go back, [y] to proceed:\t");
                      scanf(" %c",&opt);break;
                      case 3:
                             printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                             printf("\nNow enter how many bedrooms you wish your house will be on the first floor:\t");
                             scanf(" %d",&Bed1);
                             printf("\nNow enter how many bedrooms you wish your house will be on the second floor:\t");
                             scanf(" %d",&Bed2);
                             printf("\nNow enter how many bedrooms you wish your house will be on the third floor:\t");
                             scanf(" %d",&Bed3); 
                             TB=((Bed1)+(Bed2)+(Bed3));
                           printf("\nYou have selected %d bedroom(s)",TB);
                           printf("\nAre you sure?");
                           printf("\nEnter [n] to go back, [y] to proceed:\t");
                           scanf(" %c",&opt);break;
                      default:
                              printf("Invalid Input");
                 }if((opt=='Y')||(opt=='y'))
                 break;
                 }
                 while((opt=='n')||(opt=='N')||(!((Bed1>0)&&(Bed1<=3)))||(!((Bed2>0)&&(Bed2<=3)))||(!((Bed3>0)&&(Bed3<=3))));
        system("cls");
        if(L==1)        
            switch(F)
            {
                     case 1:
                          TotExp=(lot1*LA)+(LA*sqlot)+(FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break; 
                     case 2:
                          TotExp=(lot1*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNyumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 3:
                          TotExp=(lot1*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==2)
             switch(F)
            {
                     case 1:
                          TotExp=(lot2*LA)+(LA*sqlot)+(FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 2:
                          TotExp=(lot2*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 3:
                          TotExp=(lot2*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==3)
             switch(F)
            {
                     case 1:
                          TotExp=(lot3*LA)+(LA*sqlot)+(FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 2:
                          TotExp=(lot3*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nuUmber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 3:
                          TotExp=(lot3*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==4)
             switch(F)
            {
                     case 1:
                          TotExp=(lot4*LA)+(LA*sqlot)+(FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 2:
                          TotExp=(lot4*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 3:
                          TotExp=(lot4*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==5)
                     switch(F)
            {
                     case 1:
                          TotExp=(lot5*LA)+(LA*sqlot)+(FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 2:
                          TotExp=(lot5*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     case 3:
                          TotExp=(lot5*LA)+(LA*sqlot)+(FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else
                {
                printf("\nYou selected an invalid lot");
                }
                getch();
    }
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    You could use a combination of fgets() and sscanf() to do this. E.g.
    Code:
    #include <stdio.h>
    #define MIN_LIMIT 0
    #define MAX_LIMIT 20
    
    int main(void) {
        int i;
    
        while (1) {
            char line[80];
            printf("Enter a number: ");
            if (fgets(line, sizeof(line), stdin)) {
                if (sscanf(line, "%d", &i) == 1) {
                    if (i >= MIN_LIMIT && i <= MAX_LIMIT) {
                        break;
                    }
                }
            }        
        }
    
        printf("You entered %d\n", i);
    
        return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Ah, but Scorpions, that would require the OP to test the return value of sscanf. Which is exactly what we told him to do with scanf in Message #12 of this thread: http://forums.devshed.com/c-programm...ml#post2840292.

    If he ignored that advice, why assume that he won't ignore your advice as well?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    dwise1_aol i did try testing the value of scanf, but you did not reply on my post on that thread...
    esp i dont know what to do with the else statement after
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    That is still no reason to not be testing the return value of scanf! Besides, what you had posted had no practical value and had a strange side-effect.

    Look at Scorpion's example. What do you want to do if the user enters an invalid value? You would want him to try again, right? How many times? As many times as it takes. So do you want a single-pass-through control structure like an if-else if-else ? No, you would want a looping control structure, so you can keep your user in that loop and not let him out until he gives you valid input.

    What constitutes valid input? 1) That what he inputs can successfully be converted to an integer value, and 2) that that integer value falls within an acceptable range. I would envision two loops, one embedded inside the other and with the scanf placed inside the inner loop. Assuming that the scanf is supposed to read in only one input value, to get out of the inner loop, scanf needs to return a 1; if it does not, then the user should be informed that he needs to input an integer value and then the loop calls scanf again. After the user finally enters an integer value, he leaves the inner loop and is now in the outer loop where the integer value is tested for being neither too small nor too large. If it is valid, then you exit the outer loop and may proceed, but if it is not a valid value then you remain in that loop (after issuing the appropriate error message), which brings you right back into that inner loop and the scanf.

    That's how you should be thinking about this problem.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Forgot to mention the reason I'm using fgets()/sscanf() combination instead of just scanf(). The reason is because of the while loop. The problem with scanf() is that if the input is bad (e.g. user typed letters when it expected to read digits), it doesn't clear the input buffer. Therefore, a loop like this:
    Code:
    while (1) {
        printf("Enter a number: ");
        if (scanf("%d", &i) == 1) {
            break;
        }
    }
    will loop for ever if the user enters "abcd".

    Hence, we use fgets() to read whatever the user entered, into a char buffer. This allows the input buffer to be cleared even if the user typed letters, since we're reading it into a char buffer. Then we use sscanf() to examine the char buffer and see if we can read an int from it.

    BTW there are a couple of small holes in my code (like if you enter "123abc", it will read 123 and discard the rest), but you should have no trouble fixing that :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    thanks for the replies....

    i did what you said dwise1_aol loop within a loop but as scorpion mentioned it goes to infinite loop..that's why i discarded the testing....

    can i have fgets and sscanf switched?? or not?!??!

    thanks!!
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Your program, archael07, makes me sad because it has so much duplicate code. And you're lost.
    "can i have fgets and sscanf switched?? or not?!??!"
    Let's pretend you mean "can I exchange fgets and sscanf?".

    fgets makes data from the outside world available to your program.

    sscanf operates on data already available to your program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by archael07
    can i have fgets and sscanf switched?? or not?!??!
    It is completely not clear what you are asking here. If there is a human-language problem, then a sample of the kind of code that you're asking about would help, since C is our common language and it is unambiguous.

    Others think that you are asking whether it matters what order you call fgets and sscanf in; eg is
    Code:
        sscanf();
        fgets();
    as valid as
    Code:
        fgets();
        sscanf();
    The answer, already given, is that, no, they would not be the same. sscanf needs an input string to scan, while fgets provides that input string, so you would need to call fgets before you call sscanf.

    But I suspect that is not your question. I suspect that you are asking about whether you can substitute scanf with fgets and sscanf. The answer to that question would be "yes". Just remember that the return value of sscanf is the same as that of scanf.

    If that's still not your question, try asking it again, this time with some code examples of what you're wanting to do.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    thanks for your understanding haha
    yeah i get those now (fgets and sscanf) :)

    b49P23TIvg..i would love to have it shorter...yet i don't know how...still at the basics...
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    the layout is bad i know..but thanks...it works the way i want it to..thanks for the patience

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<iostream>
    #include<ctype.h>
    #define br 800
    #define sqlot 500
    #define sqfl 600
    #define lot1 1000
    #define lot2 1500
    #define lot3 2000
    #define lot4 2500
    #define lot5 3000
    #define lotmin 10
    #define lotmax 500
    #define bedmin 1
    #define bedmax 10
    #define floormin 1
    #define floormax 3
    main()
    {
          system("color 37");
          char count;
        for(count=0;count=10;count++) 
        {
            char opt,l[10],f[10],la[10],fa[10],bed1[10],bed2[10],bed3[10],tb[10],totexp[10];
            int L,F,LA,FA,Bed1,Bed2,Bed3,TB,TotExp;
            A:
                system("cls");
            printf("\n\n\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +   This program will calculate the expense of building a house.\t   +");
            printf("\n   +   \t\tAlways verify choice before proceeding.\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
            system("pause");
            system("cls");
            printf("\n\n\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +   Input the required input then press[ENTER] key...\t           +");
            printf("\n   +   Pressing an incorrect input will close this program.\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +\t\t\t\t\t\t\t\t\t   +\n   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
            system("pause");
            system("cls");
            while(10)
                      {
                                               system("cls");
                                               printf("\nPlease select desired lot by pressing the corresponding number");
                                               printf("\n[1]Sampaguita Drive");
                                               printf("\n[2]Mahogany Boulevard");
                                               printf("\n[3]Diamond Street");
                                               printf("\n[4]Moon Valley");
                                               printf("\n[5]Heaven's Gate:\t");
                                               if(fgets(l,10,stdin)){
                                               if (sscanf(l,"%d",&L)==1);{
                                               if((L>=floormin)&&(L<=5)){
                                    switch(L)
                                             {
                                               case 1:
                                               printf("\nYou have selected:\tSampaguita Drive %d per square meter\n",lot1);break;
                                               case 2:
                                               printf("\nYou have selected:\tMahogany Boulevard %d per square meter\n",lot2);break;
                                               case 3:
                                               printf("\nYou have selected:\tDiamond Street %d per square meter\n",lot3);;break;
                                               case 4:
                                               printf("\nYou have selected:\tMoon Valley %d per square meter\n",lot4);break;
                                               case 5:
                                               printf("\nYou have selected:\tHeaven's Gate %d per square meter\n",lot5);break;
                                               default:
                                               printf("\nInvalid choice");break;
                                             }break;
                                              }
                                              }
                                              }
                                              }
                      system("pause");
                      system("cls");
            while(10)
                    {                             
                                      system("cls");
                                      printf("\nNow enter desired lot area:\t");
                                      printf("\nRange is 10-500 square meters");
                                      printf("\nPrice is %d per square meter",sqlot);
                                      if(fgets(la,10,stdin)){
                                          if (sscanf(la,"%d",&LA)==1);{
                                             if((LA>=lotmin)&&(LA<=lotmax)){
                                             break;}
                                             }
                                             }                                  
                    }
                    printf("\nYou have selected %d square meters\n",LA);
                     system("pause");
                    system("cls");
              while(1)
                    {                             
                                      system("cls");
                                      printf("\nNow enter desired floor area:\t");
                                      printf("\nRange is 10-500 square meters");
                                      printf("\nPrice is %d per square meter",sqfl);
                                      if(fgets(fa,10,stdin)){
                                          if (sscanf(fa,"%d",&FA)==1);{
                                             if((FA>=lotmin)&&(FA<=LA)){
                                             break;}
                                             }
                                             }                                  
                    }
                    printf("\nYou have selected %d square meters\n",FA);
                    system("pause");
                    system("cls");
               while(1)
                {
                              system("cls");
                              printf("\nMAXIMUN OF 3 FLOORS ONLY");
                              printf("\nNow enter how many floors you wish your house will be:\t");
                              if(fgets(f,10,stdin)){
                                          if (sscanf(f,"%d",&F)==1);{
                                             if((F>=floormin)&&(F<=floormax)){
                                             break;}
                                             }
                                             }                   
                }
                 printf("\nYou have selected %d floors\n",F);
                system("pause");
                system("cls");    
        switch(F)
                 {
                      case 1:
                            {  while(1)
                              {system("cls");
                              printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                              printf("\nPrice is %d per bedroom",br);
                              printf("\nNow enter how many bedrooms you wish your house will be:\t");
                              if(fgets(bed1,10,stdin)){
                                          if (sscanf(bed1,"%d",&Bed1)==1);{
                                             if((Bed1>=bedmin)&&(Bed1<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                      TB=(Bed1);
                      printf("\nYou have selected %d bedroom(s)\n",TB);break;
                      system("pause");
                      }
                      case 2:
                            { while(1)
                             {system("cls");
                             printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                              printf("\nPrice is %d per bedroom",br);
                             printf("\nNow enter how many bedrooms you wish your house will be on the first floor:\t");
                             if(fgets(bed1,10,stdin)){
                                          if (sscanf(bed1,"%d",&Bed1)==1);{
                                             if((Bed1>=bedmin)&&(Bed1<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                             while(1)
                             {system("cls");
                             printf("\nNow enter how many bedrooms you wish your house will be on the second floor:\t");
                             if(fgets(bed2,10,stdin)){
                                          if (sscanf(bed2,"%d",&Bed2)==1);{
                                             if((Bed2>=bedmin)&&(Bed2<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                             TB=((Bed1)+(Bed2));
                      printf("\nYou have selected %d bedroom(s)\n",TB);break;
                      system("pause");
                      }
                      case 3:
                            { while(1)
                             {system("cls");
                             printf("\nMAXIMUM OF 10 Bedrooms only per floor");
                              printf("\nPrice is %d per bedroom",br);
                             printf("\nNow enter how many bedrooms you wish your house will be on the first floor:\t");
                             if(fgets(bed1,10,stdin)){
                                          if (sscanf(bed1,"%d",&Bed1)==1);{
                                             if((Bed1>=bedmin)&&(Bed1<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                             while(1)
                             {system("cls");
                             printf("\nNow enter how many bedrooms you wish your house will be on the second floor:\t");
                             if(fgets(bed2,10,stdin)){
                                          if (sscanf(bed2,"%d",&Bed2)==1);{
                                             if((Bed2>=bedmin)&&(Bed2<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                             while(1)
                             {system("cls");
                             printf("\nNow enter how many bedrooms you wish your house will be on the third floor:\t");
                             if(fgets(bed3,10,stdin)){
                                          if (sscanf(bed3,"%d",&Bed3)==1);{
                                             if((Bed3>=bedmin)&&(Bed3<=bedmax)){
                                             break;}
                                             }
                                             }  
                              }
                             TB=((Bed1)+(Bed2)+(Bed3));
                           printf("\nYou have selected %d bedroom(s)\n",TB);break;
                      system("pause");
                      }
                      default:
                              printf("Invalid Input");
                              system("pause");
                 }             
        system("cls");
        if(L==1)        
            switch(F)
            {
                     case 1:
                          TotExp=(lot1*LA)+(LA*sqlot)+(F*FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break; 
                     case 2:
                          TotExp=(lot1*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNyumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 3:
                          TotExp=(lot1*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Sampaguita Drive\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==2)
             switch(F)
            {
                     case 1:
                          TotExp=(lot2*LA)+(LA*sqlot)+(F*FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 2:
                          TotExp=(lot2*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 3:
                          TotExp=(lot2*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Mahogany Boulevard\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==3)
             switch(F)
            {
                     case 1:
                          TotExp=(lot3*LA)+(LA*sqlot)+(F*FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 2:
                          TotExp=(lot3*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nuUmber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 3:
                          TotExp=(lot3*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Diamond Street\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==4)
             switch(F)
            {
                     case 1:
                          TotExp=(lot4*LA)+(LA*sqlot)+(F*FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 2:
                          TotExp=(lot4*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 3:
                          TotExp=(lot4*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Moon Valley\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else if(L==5)
                     switch(F)
            {
                     case 1:
                          TotExp=(lot5*LA)+(LA*sqlot)+(F*FA*sqfl)+(Bed1*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 2:
                          TotExp=(lot5*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2)*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     case 3:
                          TotExp=(lot5*LA)+(LA*sqlot)+(F*FA*sqfl)+((Bed1+Bed2+Bed3)*br);
                          printf("\nYour chosen lot is at Heaven's Gate\nLot Area:%d\nFloor Area:%d\nNumber of Floors:%d\nNumber of Bedrooms:%d",LA,FA,F,TB);
                          printf("\nThe Price is: P%d\n",TotExp);break;
                     default:
                             printf("Invalid...");
            }
        else
                {
                printf("\nYou selected an invalid lot");
                }
                system("pause");
    }
    }
  22. #12
  23. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480

    Refactor the Nyumber block.


    If programming were algebra,
    Code:
    y * x +   z * x == (y + z) * x
    (Without having tested,) I'd rewrite one of the blocks. as shown. I didn't look at a higher level to find grosser duplication. Now you have to fix the spelling of "Nyumber" in one place and you'll have a chance to find the problem.
    Code:
    if (1 == L) { /* always put constant on left of comparisons involving = sign */
      if ((F < 1) || (3 < F))	/* place shorter block of the condition first */
        fputs("Invalid...",stdout);	/* fputs runs more efficiently than printf */
      else {
        int Beds = 0;
        switch (F) {		/* fall-through switch is perfect here */
        case 3: Beds = Bed3;
        case 2: Beds += Bed2;
        case 1: Beds += Bed1;
        }
        TotExp = LA * (lot1 + sqlot) + (FA * sqfl) + (Beds * br); /* algebraic factoring */
        /* as I recall sequential strings are concatenated.  Thus I reduce source code width */
        printf("\nYour chosen lot is at Sampaguita Drive\n"
    	   "Lot Area:%d\n"
    	   "Floor Area:%d\n"
    	   "Number of Floors:%d\n"
    	   "Number of Bedrooms:%d",LA,FA,F,TB);
      }
    }
    Last edited by b49P23TIvg; December 15th, 2012 at 08:10 AM. Reason: Insert title, improve spelling.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    i rearranged it as you explain "concatenated"....it looks better now,,for me anyways..just a little problem i found..

    if I input correct values...it will run BUT after looping and I tried entering invalids(alphabets etc.) on the second run, the program accepts and enters the values i entered in the first run of the program
    example

    ENTER floor area ...(20)<<first run
    ENTER floor area ...(a)<<second run
    it will display "You selected 20"....on the second run

    so i removed the for loop at the beginning for now...
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Are you by any chance taking about this section of code?:
    Code:
             while(1)
                    {                             
                                      system("cls");
                                      printf("\nNow enter desired floor area:\t");
                                      printf("\nRange is 10-500 square meters");
                                      printf("\nPrice is %d per square meter",sqfl);
                                      if(fgets(fa,10,stdin)){
                                          if (sscanf(fa,"%d",&FA)==1);{
                                             if((FA>=lotmin)&&(FA<=LA)){
                                             break;}
                                             }
                                             }                                  
                    }
                    printf("\nYou have selected %d square meters\n",FA);
                    system("pause");
                    system("cls");
    In the line testing the return value of sscanf, please note the semicolon you had put in there that I have colored red. What you are telling that compiler there is that if the sscanf test is true, then do [u]nothing[/u. Of course, that's also what you're telling it to do if that test comes out as false. In addition, you're telling the compiler to execute that subsequent code block every single time! Now, assuming that you are not changing the value of FA anywhere else, then when you enter a bogus input, then sscanf will fail and the value of FA will not be changed; the value of FA will still be what you had entered in the previous run. And because of that extraneous semicolon, you will use that previous value this second time.

    Here's your main problem: your indenting style is all frakked up and it's frakking up your attempts to understand your own code. You hid that extra semicolon away where you could not see it and, instead of finding and correcting the real problem, you ended up making counter-productive changes without thinking, but rather out of panic. Your frakked-up indenting style is causing you to shoot yourself in the foot.

    Read and learn about the Allman style at http://en.wikipedia.org/wiki/Indent_style#Allman_style. It is a very good style that I recommend very highly. Here is what that frakked-up code looks like when indented properly:
    Code:
            while(1)
            {                             
                system("cls");
                printf("\nNow enter desired floor area:\t");
                printf("\nRange is 10-500 square meters");
                printf("\nPrice is %d per square meter",sqfl);
                if(fgets(fa,10,stdin))
                {
                    if (sscanf(fa,"%d",&FA)==1);
                    {
                        if((FA>=lotmin)&&(FA<=LA))
                        {
                            break;
                        }
                    }
                }                                  
            }
            printf("\nYou have selected %d square meters\n",FA);
            system("pause");
            system("cls");
    You can tell which braces belong where. You can tell what the structure of the code is. And you can see that extra semicolon stuck at the end of an if statement where it does not belong! If you had used a proper indenting style, then you would have been able to spot your mistake on your own and been able to correct it without panicking and screwing up your code with unnecessary changes.

    Properly indenting your code is mostly for your own benefit! Stop shooting yourself in the foot!
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0
    no problem...i already did the indenting part but have not posted the new code....right now its like what you've posted....so i'll just omit the ;

IMN logo majestic logo threadwatch logo seochat tools logo