0

In Cash of Problem Set 1, I'm quite sure that I've declared the variables used in the code, but I keep getting the error :

error: use of undeclared identifier 'q'

int coins =   (q + d + n + p) ;

I have declared the variables used

However in the final step of adding them together in the command : int coins = (q + d + n + p) ; There seems to be an error, but I'm not being able to figure out


Complete code:

#include <stdio.h>

#include <cs50.h>

#include<math.h>

int main (void)

{



      //propmting user for input
    float dollars;
    do
    {
   dollars = get_float("Change owed (in dollars) : ");
    }
    while (dollars<0);

    //Convert amount in dollars to cents,rounding it.

    int cents = round(dollars * 100);

    //Calculate no of coins required to provide required change

int q=0;

do

 {

     q ++;
     cents = cents%25;
 }
 while (cents>=25);

int d=0;
 do
 {
     d ++;
     cents = cents %10;
 }
 while (cents<25&&cents>=10);

int n=0;
 do
 {

    n ++;
    cents =  cents % 5;
 }
 while (cents<10&&cents>=5);

int p=0;
 do
 {
     p ++;
     cents = cents % 1;
 }
     while ( cents<5&&cents>=1);
}

//Adding up no of coins

int coins =   (q + d + n + p) ;

//print out no of coins

printf("%i\n",coins);
2
  • This could be because the declaration of q and the line that uses it are not in the same scope. You'll need to post more of your code to find the cause of this error. Feb 8 at 18:27
  • @Robert S. Pratt I have added the complete code to the question. It would be great if you could take a look! Feb 8 at 18:54
1

Your int coins = (q + d+ n + p); line is outside of your main() function. This is also true for your final printf statement. You need to move your final } to be the last thing in your code. This should solve your problem.

2
  • Thank you so much ! Feb 8 at 19:04
  • Glad to help. : ) Don't forget to accept the answer, by clicking the the check next to it, if it solved your problem. Feb 8 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .