I’m not getting C. Please help me!

Asked

Viewed 90 times

-3

I’m looking to make an ATM with only 10 notes, with a limit of 1000 ballots. However, you have to show me how many ballots are in the box and how much is left to remove, and that it is possible to withdraw as many times as you want until the notes run out. However, when I put the amount I want to withdraw the program does not show the amount of ballots withdrawn and when I answer anything the program closes, and it could only be closed when I answered "N".

#include<stdio.h>
main()
{
char R[1];
int S, SQ, Nc, Vt;
   Nc=1000;
   Vt=10000; 
    while(R!="N" || Nc>0)
    {
    printf("limite:%d",Vt);
    printf("\nDigite quanto deseja sacar:");
    scanf("%d",&S);
    SQ=S/10;
    if ((S<0) || (S>Vt) || (S%10!=0))
    printf("Valor invalido");
    printf("\nDeseja realizar outro saque? [S/N]");
    scanf("%s",&R);
    break;
    if ((S%10==0) && (S<Vt))
    printf("%d nota(s) de 10.", SQ);
    printf("Deseja realizar outro saque? [S/N]");
    scanf("%s",&R);
    Vt=Vt-S;
    Nc=Nc-SQ;
    break;
    if (Nc == 0)
    printf("Sem notas");
    break;
}
}
  • 1

    We who ask: where is the error? What is happening?

  • Seems related to me: https://answall.com/q/386706/3774

  • The program fails, but after we respond as we wish to withdraw the program.

  • Maybe that’s the problem if (Nc = 0) one more comparison = instead of ==

  • 1

    I just made the change, but the program keeps closing after I put the loot value.

  • 1

    Ok, so now it’s only worth you [Edit] the question and put the new code fixed and maybe add more information like: What is going on, Shows an error, Steps for error to occur, ... This will help the community identify and explain what is happening =D -- don’t forget to take a look at our [Tour]

  • correct the indentation of your code, to facilitate the understanding of other people

Show 2 more comments

2 answers

0

The mistake you’re making is: segmentation fault (core dumped)

This error is caused on this line: scanf("%d",S);

Missed the "&": scanf("%d",&S);

If you asked the question stating this error, and where it is occurring, surely several users would identify the error at the same time!

  • There is even a logic error: no while(R!="N" || Nc>0) should be &&, otherwise your program will only stop when the user type 'N' and Nc is less than or equal to 0

  • I fixed this error, but the program it starts, I put the amount I want to withdraw, but it does not say how many ballots were removed and when I press to continue it closes the program, and was only to end when I put "N" as answer.

  • Fixed which error? What error is occurring now?

-1

Your code has not been posted complete.

Or you declare your variable R with a single character and make the comparison using single quotes:

char R;
...
while(R!='N' || Nc>0) { 
...
scanf("%c",&R);

or declares as a string, this is an array of characters that must have a minimum size of 2 to hold the terminator character ' 0'. In this case you will need to use the strcmp function of <string.h> and do not use & in scanf.

char R[2]; 
... 
while(strcmp(R, "N") != 0 || Nc>0) { 
...
    scanf("%s",R); 

Note that it makes no sense for you to put commands in the sequence of a break; because they will not be executed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.