Click here to Skip to main content
15,884,605 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Can you please tell me what's wrong in my code. The program crashes after the user enters his name! Its just a program created for fun but i want to know what's wrong with it!
C++
    #include<<stdio.h>>
#include<<conio.h>>
int main()
{
    char name;
    int answer1;
    int answer2;
    int answer3;
    printf("What is your name?\n");
    scanf("%s", name);
    printf("\nOh wow, What a wonderful name!\n Hello %s, What can i do for you???\n", name);
    printf("\nDo you want me to make something for you???[YES=1 OR NO=2]\n");
    scanf("%d",&answer1);
    if(answer1==1)
    {

        printf("\n Do you want something to eat or do you want something to drink???\n[EAT=1 OR DRINK=2]\n");
        scanf("%d",&answer2);
        if(answer2==2)
        {

            printf("I can make a coffee or a cup of tea for you!\n What do you prefer?[coffee=1 or tea=2]");
            scanf("%d",&answer3);
            if(answer3==1)
            {
                printf("\nHere is the recipe:\n");
                printf("The Right Equipment\nCarguilo likes the clean, robust taste that comes from a manual dripper, since it filters out oil and sediment. And she’s not alone. As basic as it is, the pour-over has become the latest thing at gourmet coffee shops. Cone-shaped drippers work fine, but Carguilo prefers the flat base of the Kalita Wave (shown at right, $38; filters, $13 for 100: shop.wreckingball coffee.com). "The grounds lie evenly, so the water saturates them equally," says Carguilo.\nThe Right Water\nIf you don’t like the flavor of your tap water, use filtered or bottled.\n\nFresh Grounds\nGrind beans within a half hour of brewing. A burr grinder is ideal but pricey ($50 or more). It creates uniform grounds and prevents the coffee from ending up too weak or too bitter. For a manual dripper, medium size (similar to raw sugar granules) is best. (Learn how to choose the best gourmet coffee beans.)\nMagic Ratio\nTo brew 16 ounces of coffee (two big cups), use 5 tablespoons (or 28 grams) of coffee and 16 ounces of water.\nMaster the Pour-Over\n\n\nTotal brew time: 3 to 5 minutes\n1. As your kettle heats, place a dripper lined with a paper filter on a mug or a carafe. Rinse the filter with hot water to get rid of paper dust and to preheat the cone.\n2. Place ground coffee in the dampened filter.\n3. After the water boils, wait 10 seconds for it to settle. Slowly pour just enough hot water (in a circular motion) to saturate all the grounds.\n4. Pause 30 seconds to let the coffee "bloom." It will bubble and soften.\n5. Pour again, raising the water level to an inch above the grounds. Wait a few moments until\nthe water trickles through the dripper. Repeat this process of "pulse pouring," which helps prevent overflow, until you have your desired amount of brewed coffee\n.");
                printf("\n\nHAHAHAHAHAHHAHA...here's the recipe...go make it yourself. DAMN...you are so lazy that you want a computer to make a coffee for you hahahahaha!");

            }
            if(answer3==2)
                {
                    printf("Boil water.\n Take 3/4th cup for every cup you want to make. The measure of water is important for the taste you want to achieve\n\nNow add tea powder according to number of cups. You can take one teaspoon per cup. If you want the tea to be strong then add more powder. If you wish to keep it light lessen the amount of tea powder\n\nNow add sugar as per your taste buds.\n\nBring the mixture to a boil. Stop when you see a reddish tinge in the concoction. Boiling further would give it a bitter taste.\n\nSieve and pour in cups.\n\nAdd warm milk for milk tea. For tea without milk add some ginger drops or lime, according to your preference\n\n\nHAHAHHHAHAHA...you are so lazy that you want a computer to make a cup of tea for you...HAHAhA...here'sthe recipe\n\nGO MAKE IT YOURSELF HAHAHAHAHHA");
                }
                else
                    printf("");

        }
        if(answer2==1)

            {
                printf("\nOH GOD!!!\n\n\n\nLOOK AT YOU\n\nYou've become so fat...still you dont feel ashamed of asking me to make something for you to eat\n\nGO JOIN A GYM!");
            }
            else
                printf("");


    }
    if(answer1==2)

        {
            printf("Oh %s, if you want nothing to eat...then im of no use...\nbecause i am only good at making something for my master\n\nif you want me to make anything for you;\nsimply, run the program again!Thank you %s...\nHAVE A GOOD DAY", name, name);
        }
        else
            printf("");
}

Thank You for your help! Have a nice day! :)
Posted
Updated 23-Jul-15 2:53am
v4
Comments
[no name] 23-Jul-15 8:52am    
Once you get your code to compile, try scanf("%s", &name);

C#
char name;
//..
scanf("%s", name);

is wrong.
A string is not a single char, is a char array.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Jul-15 15:23pm    
Sure, a 5.
—SA
you produced a basic bug in providing not enough memory for the input.

C++
char name[50];//char array can hold up to 49 chars and a zero for the end
memset( name,0,50);//clean buffer


Read some basic tutorials ;-)
 
Share this answer
 
Look at your code:
C++
char name;
...
scanf("%s", name);
You have allocated a single byte of space to hold the user's name and then read a string into it! What happens then, is that the framework obeys your command, and fills the character and the following memory with the name - which overwrites the return address on the stack for the scanf function call, so it tries to return to a random address and crashes.

Try declaring name as an array of characters:
C++
char name[100];
...
scanf("%s", name);
And see if that works any better!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900