-3

I'm sure there are probably tons of syntax/other errors, but I'm trying to figure out the two that it's picking up on. I'm very new to this so I don't really know how to fix undeclared identifiers. Note that #include <cs50.h> is just CS50's library.

#include <cs50.h>
#include <stdio.h>

int main (void)
{ 
   int add, fee, disc;
   printf("For rate with tax and Security Deposit, type y. For 10 percent off, type n:"); 
   string name = GetString();
   if (name == y)
   { 
     printf("PreTax Amount: ");
     scanf("%d", &fee);
     printf("Okay. I will add the 10 percent tax to %d.\n ", fee);

     add        = (1.1 * fee);

     printf("Plus Tax Amount = %d\n", add);
     printf("Security Deposit = 1000 dollars\n");
     printf("Total = (%d + 1000)", add); 
   }  
   else if (name == n)
   {
     printf("PreTax Amount: ");
     scanf("%d%d", &fee, &disc);
     printf("Okay. I will minus the 10 percent discount to %d and then add tax.\n ", fee);

     add        = (0.9 * fee);
     disc       = (add * 1.1);

     printf("Minus Discount Amount plus tax = %d\n", disc);
     printf("Security Deposit = 1000 dollars\n");
     printf("Total = (%d + 1000)", disc);
   }

   return 0;
}

errors:

ContractualHelper.c:10:17: error: use of undeclared identifier 'y'
if (name == y)
            ^
ContractualHelper.c:22:22: error: use of undeclared identifier 'n'
else if (name == n)
                 ^
2 errors generated.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
cb61899
  • 1
  • 1
  • 1
  • 1
  • The compiler is saying "I don't know what's `y`." – zneak Apr 21 '14 at 22:48
  • 3
    The same error twice: you want to compare against the literal character `'n'` or the literal string `"n"`, not "a *variable* called `n`". Names of variables don't mean anything to running code; C is not introspective. – Jongware Apr 21 '14 at 22:48

2 Answers2

1

y and n are undeclared because they do not have definitions.

int y; is a declaration, which would get rid of that error.

However, your code

if (name == y)

is comparing a variable name to a variable y, and I think what you want to do is see if name contains the string y.

How to do that comparison is another issue.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • 2
    hint: strcmp, strncmp (http://www.cplusplus.com/reference/cstring/strncmp/), or operator[] – Peter - Reinstate Monica Apr 21 '14 at 23:04
  • @PeterSchneider So does strcmp ask "What is contained in variable 'name'"? thanks for the feedback :) – cb61899 Apr 24 '14 at 02:55
  • Strcmp takes arguments of two strings, and tells you if they are the same. You can send those in as variables. – Almo Apr 24 '14 at 12:37
  • 1
    @cb61899: Did you read the man page I linked to? Which part is it that you didn't understand (I'm not being sarcastic)? Do you understand what C "strings" are at all? Btw, I realize that you use a GetString() function with a type string which is not part of standard C; is it the same as here: http://stackoverflow.com/questions/15892003/c-string-from-getstring-when-strlen-produces-segmentation-fault ? Then my somewhat flippant pointers to st[n]cmp are valid (because `string` appears to be just a typedef for `char *`). – Peter - Reinstate Monica Apr 24 '14 at 13:20
0

Since you want just to compare with either 'Y' or 'N' I suggest you use char instead of string. so : char name;

scanf("%c", &name);

than when comparing use :

if (name == 'Y') ...

ADD

you also should consider wrong inputs from user so add another else like :

#include <stdio.h>
#include <string.h>

main()
{
   char name;
   scanf("%c", &name);

   if (name == 'y') 
    printf("Yes");

   else if (name == 'n')
    printf("No");

   else 
        printf("wrong input");
}
chouaib
  • 2,763
  • 5
  • 20
  • 35
  • Using `string` makes the input process a lot simpler, he doesn't have to worry about newlines left in the input stream and other such guff – M.M Apr 21 '14 at 23:57
  • Yes sure, but you could just follow his path and deduce that he wants a one over two possible flows "yes or no", i guess it's for class assignment – chouaib Apr 22 '14 at 00:01
  • Thanks for the advice :) I'm actually just using cs50's VMWare to make this for the contracts I write up for my mom's vacation rental business. It's not an actual class project, so no plagiarism rules apply. – cb61899 Apr 24 '14 at 03:04
  • @cb61899 Respect for u mate, i wish my mom appreciates me doing some programming for her also ;) anyway no worry we are in programming QA site there is no plagiarism risk – chouaib Apr 24 '14 at 04:18