0

I'm currently in the process of learning my first programming language, C. I'm on the 3rd chapter of my book, titled "Conditions", so I only know as much as the book has taught me up until this point (basically, I'm sorry if this seems like a dumb question).

I've been writing random bits of code to try and follow along, and everything was going fine until the last section of the code. For whatever reason, when I compile and run it, it skips over the scanf portion. But when I isolate the code in a new file and run it, it works like its supposed to. The rest of the code before that last section works as well.

I feel like I've looked it up and down, but I can't figure out what's wrong. I'm sure its because I still have so much to learn but if someone could please help me pinpoint it, I'd really appreciate it! Here's the code below (also sorry if it sounds corny, I was just trying to entertain myself, didn't think I'd need anyone to see it).

#include <stdio.h>

int main ()
{   
    int iResponse = 0;
    int iResponse2 = 0;
    int iSelection = 0;
    int iArchers = 0;
    int iSwordsmen = 0;
    float fTransAmount = 0.0;
    float fBalance = 100.00; 
    char cResponse = '\0';//KEEP THIS ONE IN MIND

printf ("\nThe Dragon is striking again! How do you respond?\n");
printf ("\n1.\tJump out of the way");
printf ("\n2.\tCounter Attack");
printf ("\nEnter your Selection: ");
scanf ("%d", &iResponse);



//  - Basic if/else structure

if (iResponse == 1) 
{
    printf ("\nDodged!\n"); 
}
else 
    printf ("\nAttack!\n");


printf ("\n\nThe Dragon has damaged you some!");
printf ("\n\tIn-Battle Healing\n");
printf ("\n1.\tDrink Health Potion\n");
printf ("2.\tResume Battle");
printf ("\nEnter your Selection: ");
scanf ("%d", &iResponse2);

if (iResponse2 == 1)
{
    printf ("\nDrinking Health Potion!\n"); 
}
    
if (iResponse2 == 2)
{
    printf ("\nResuming Battle!\n");        
}
    

//  - Example from the book of a simple banking program:

printf ("\n\nYou've sucessfully defeated the Dragon! In Return, the King has awarded you 500 gold! Go to the bank and deposit the gold.\n");
printf ("\n**************************************************************************************************************************************");
printf ("\n\tWELCOME TO YE OLDE BANK\n");
printf ("\n1.\t Deposit Gold\n");
printf ("\n2.\t Withdraw Gold\n");
printf ("\nEnter your selection: ");
scanf ("%d", &iSelection);

if (iSelection == 1)
{
    printf ("\nEnter the amount of gold to deposit: ");
    scanf ("%f", &fTransAmount);
    printf ("\nYour new balance is: $%.2f gold\n", fBalance + fTransAmount);
}

if (iSelection == 2)
{
    printf ("\nEnter the amount of gold to withdraw: ");
    scanf ("%f", &fTransAmount);
    if (fTransAmount > fBalance)
        printf ("Insufficient funds\n");
    else 
        printf ("\nYour new balance is: $%.2f", fBalance - fTransAmount);
}



//  - example using the && (and) operator:
printf ("\n");
printf ("\n");
printf ("\n**************************************************************************************************************************************");
printf ("\n\nBecause of your success defeating the dragon, the King has requested your services again.");
printf ("\n\nKing:\t\"Hello Traveler! I am need of your services once again. I'm having trouble dealing with those pesky smugglers in New Haven\"");
printf ("\n\t\"I would like for you to ride into New Haven, discover their lair, and defeat the smugglers. Then return the goods to me.\"");
printf ("\n\t\"This job won't be easy, so I am lending you some of my men. How many swordsmen and archers will you require?\"");
printf ("\n\nEnter the amount of swordsmen you will need: ");
scanf ("%d", &iSwordsmen);
printf ("\nEnter the amount of archers you will need: ");
scanf ("%d", &iArchers);

if (iSwordsmen <= 5 && iArchers <= 5)
{
    printf ("\nKing:\t\"That won't be enough men! I'll lend you some more.\"");
    printf ("\n\nYou now have 10 swordsmen and 10 archers");
}

if (iSwordsmen <= 5 && (iArchers <= 10 && iArchers >5))
{
    printf ("\nKing:\t\"That won't be enough swordsmen! I'll lend you some more.\"");
    printf ("\n\nYou now have 10 swordsmen and %d archers", iArchers);
}

if ((iSwordsmen <= 10 && iSwordsmen >5) && iArchers <= 5)
{
    printf ("\nKing:\t\"That won't be enough archers! I'll lend you some more.\"");
    printf ("\n\nYou now have %d swordsmen and 10 archers", iSwordsmen);
}

if (iSwordsmen > 10 && iArchers > 10)
{
    printf ("\nKing:\t\"I can't spare that many men! I can give you 10 of each.\"");
    printf ("\n\nYou now have 10 swordsmen and 10 archers");
}

if ((iSwordsmen <= 10 && iSwordsmen > 5) && iArchers > 10)
{
    printf ("\nKing:\t\"That's too many archers! I can give you 10 at most.\"");
    printf ("\n\nYou now have %d swordsmen and 10 archers", iSwordsmen);
}

if (iSwordsmen > 10 && (iArchers <= 10 && iArchers > 5))
{
    printf ("\nKing:\t\"That's too many swordsmen! I can give you 10 at most.");
    printf ("\n\nYou now have 10 swordsmen and %d archers", iArchers);
}

if (iSwordsmen <= 5 && iArchers > 10)
{
    printf ("\nKing:\t\"That won't be enough swordsmen and that's too many archers! How about I give you 10 of each.\"");
    printf ("\n\nYou now have 10 swordsmen and 10 archers");
}   

if (iSwordsmen > 10 && iArchers <= 5)
{
    printf ("\nKing:\t\"That's too many swordsmen and that won't be enough archers! How about I give you 10 of each.\"");
    printf ("\n\nYou now have 10 swordsmen and 10 archers");
}

if ((iSwordsmen > 5 && iSwordsmen <= 10) && (iArchers > 5 && iArchers <= 10)) 
{
    printf ("\nKing:\t\"Wonderful!\"");
}
printf ("\n\nKing:\t\"Here's all the information we have on the smugglers\"\n\t\"Please look it over while I gather your team\"");
printf ("\n\n**********************************************************************************************************************************");

// - Example using the || (or) operator: (This is where it seems to mess up, and skips the scanf part when I run it. But when I isolate it in a new file and run it there, it works just fine)

printf ("\nYour Chief Startegist has looked over the information the King provided and has come up with 3 different lines of attack:");
printf ("\n");
printf ("\nA)\tDirect attack through the main entrance of the lair");
printf ("\nB)\tSneack attack through the back entrance of the lair");
printf ("\nC)\tCreate a diversion, then initiate the attack");
printf ("\n\nChief Strategist:\t\"Which plan would you like you like to proceed with?\"");
printf ("\nEnter your response here: ");
scanf ("%c", &cResponse); //This is where it seems to mess up

if (cResponse == 'A' || cResponse == 'a')
{
    printf ("\nChief Strategist:\t\"This plan will be difficult to execute. I'll prepare the men\"");
}

if (cResponse == 'B' || cResponse == 'b')
{
    printf ("\nChief Strategist:\t\"This plan will be tricky, but I think we can manage it. I'll inform the men\"");
}

if (cResponse == 'C' || cResponse == 'c')
{
    printf ("\nChief Strategist:\t\"I think this would be the best plan of attack. I'll inform the men\""); 
}

return 0;
}
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
  • 1
    The post title should describe your question or problem well enough so that readers get the gist of what it's about ("Problem with Java function" is not very descriptive). Keep it succinct. You don't need to mention things that are already tagged, such as the programming language. See: [How do I write a good title?](https://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title) – John Kugelman Sep 18 '21 at 23:51

0 Answers0