• 0

swich, no output


Question

Hello everyone.  I'm attempting to write a program that prompts the user for a quantity of partID's and then use a switch (loop) to id the part's unitprice for x amount of parts.  then display the total for those parts.  What I'm trying to do right now is just get the switch (loop) to work.  I'm not exactly worried about the last part of the total right this second.  What I have is :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main()
{
	int partID = 0;
	double unitprice = 0;
	int ch = 0;

	printf("Please enter partID\n");
	printf("Please enter q to quit\n");

	ch = getch();

	do{
			switch(partID)
		{
		case 1:
			unitprice = 1.30;
			break;
		case 2:
			unitprice = 0.99;
			break;
		case 3:
			unitprice = 2.50;
			break;
		case 4:
			unitprice = 1.42;
			break;
		case 5:
			unitprice = 1000000.00;
			break;
		default:
			printf("Part cannot be found\n");
		case 0:
			partID++;
			break;
			}
	}
				while (partID != 0);
			

}

this is what it's outputting : (see picture below)

  which is outputting nothing but the printf's .  it wont even access the loop, nor will it exit.  what am I don't wrong?

post-505691-0-00288900-1380473727.png

Link to comment
Share on other sites

Recommended Posts

  • 0

You're never comparing "ch" to anything and only the default switch case has any output.

Get rid of the do...while loop and just use switch like so:

switch(ch)
{
	case 1:
		unitprice = 1.30;
		break;
	case 2:
		unitprice = 0.99;
		break;
	case 3:
		unitprice = 2.50;
		break;
	case 4:
		unitprice = 1.42;
		break;
	case 5:
		unitprice = 1000000.00;
		break;
	default:
		printf("Part cannot be found\n");
		break;
}
You can then use an if statement to test if unitPrice is > 0 and output it afterwards.
  • Like 1
Link to comment
Share on other sites

  • 0

As Greywolf said, you're not using the right value in the switch statement. You're doing your switch using the value inside "partID", but entering your input into "ch". So partID will always have a value of zero!

The reason your application is not exiting is because you're stuck in an infinite loop. Because partID is always zero, you follow the zero case, which increments partID by one ("partID++"). Then it breaks out of the case statement and because partID isn't zero (it's now 1 because of partID++), you meet the loop condition and it loops round again, this time executing case 1, which sets unitprice to 1.30. From then on, partID will only ever be "1", and you never break out of the loop. You're effectively stuck until the end of time (or you kill the program).

The reason you're not seeing any output is because you're not doing anything to show output. You're just setting a variable value. Like Greywolf said, you'll need a printf statement to show the value of unitprice!

Also, because you're using a loop, I presume you mean't to put the line "ch = getch();" inside the loop?

Link to comment
Share on other sites

  • 0

After much help from a friend we've run unto another roadblock.  Instead of returning the decimal points it is truncating it into a whole number answer, when it shouldn't.  This is what we have

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()


{

int input = 0;

do{


int partID;

long double quantity = 0.0; // we want to get the quantity ordered and also we want to make sure to make it a double for multiplying because we want to multiply doubles by doubles
long double unitPrice = 0.0;// it is good practice to capitalize the first letter of next word

printf("Please enter partID: ");
scanf("%d", &partID);

printf("\n\nPlease enter quantity: ");
scanf("%d", &quantity);

switch(partID)
{

case 0:    
unitPrice = (1.30 * quantity); 
printf("Total: %d\n", unitPrice);
input++;
break;

case 1:
unitPrice = (0.99 * quantity); //  multiply the quantity times the price to get total
printf("%d of item %d at %d each = %d\n\n\n", quantity, partID, unitPrice);
printf("Total: %d\n", unitPrice);
input++;
break;

case 2:
unitPrice = (2.50 * quantity);
printf("%d of item %d at %d each = %d\n\n\n", quantity, partID, unitPrice);
printf("Total: %d\n", unitPrice);
input++;
break;

case 3:
unitPrice = (1.42 * quantity);
printf("%d of item %d at %d each = %d\n\n\n", quantity, partID, unitPrice);
printf("Total: %d\n", unitPrice);
input++;
break;

case 4:
unitPrice = (1000000.00 * quantity);
printf("%d of item %d at %d each = %d\n\n\n", quantity, partID, unitPrice);
printf("Total: %d\n", unitPrice);
input++;
break;

default:
printf("Part cannot be found\n");
input++;
break;

}
}while (input < 1); 

}// This whole thing keeps returning a whole number. I cannot seem to get it to return a decimal.

This is what it's doing:  (see attached) any ideas as to why?

post-505691-0-36342100-1380485586.png

Link to comment
Share on other sites

  • 0

You are telling printf() to interpret your floating point numbers (quantity and unitPrice) as integers, hence the loss of precision. According to the printf() documentation you should be using "%f" to interpret your floating point numbers instead of "%d". For example:

printf("%f of item %d at %f each = %f\n\n\n", quantity, partID, unitPrice, quantity*unitPrice);

If you want to limit the output of your floating point numbers to two decimal places - a common request - you can do so by specifying their precision for printf(). For example:

printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice, quantity*unitPrice);
Link to comment
Share on other sites

  • 0

You're using %d in your printf, which shows output as an integer value, you want to use %f, which displays as floating point output:

 

printf("%d of item %d at %f each = %f\n\n\n", quantity, partID, unitPrice);
//                        ^        ^
//                        See here (and here)
A full list of printf format specifiers can be found here: http://www.cplusplus.com/reference/cstdio/printf/
Link to comment
Share on other sites

  • 0

Okay so I use %.02f and that will give me the two places after the decimal?

 

No. Read the printf() documentation Majesticmerc and I linked to. "%4.2f" will give you two places after the decimal, just like in my example above. To expand on this further, the "4" tells printf() to print at least 4 characters. For example, "3.14" is exactly four characters. The ".2" tells printf() to print the floating point number rounded to exactly two decimal places. Therefore by requiring at least four characters and exactly two digits after the decimal, you ensure that you will always have either a natural number or a zero before the decimal point. For example, ".14" is not possible; it would be printed as "0.14" instead. I hope my explanation makes sense to you.

Link to comment
Share on other sites

  • 0

No. Read the printf() documentation Majesticmerc and I linked to. "%4.2f" will give you two places after the decimal, just like in my example above. To expand on this further, the "4" tells printf() to print at least 4 characters. For example, "3.14" is exactly four characters. The ".2" tells printf() to print the floating point number rounded to exactly two decimal places. Therefore by requiring at least four characters and exactly two digits after the decimal, you ensure that you will always have either a natural number or a zero before the decimal point. For example, ".14" is not possible; it would be printed as "0.14" instead. I hope my explanation makes sense to you.

thank you for the explanation, that makes perfect sense to me now! However now I am having problems with the output once again.  instead of getting extremely large numbers (like I was getting for a while) now I am getting 0.00 instead.  Not sure if I did that on accident by changing from %d to %4.2f.  Here is what I have code wise ...it will not allow me to attach a picture though. 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()


{

int input = 0;

do{


int partID;

double quantity = 0.0; 
double unitPrice = 0.0;
printf("Please enter partID: ");
scanf("%d", &partID);

printf("\n\nPlease enter quantity: ");
scanf("%d", &quantity);

switch(partID)
{

case 0:   
unitPrice = (1.30 * quantity);
printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice);
printf("Total: %d\n", unitPrice);
input++;
break;

case 1:
unitPrice = (0.99 * quantity); // we want to multiply the quantity times the price to get total
printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice);
printf("Total: %f\n", unitPrice);
input++;
break;

case 2:
unitPrice = (2.50 * quantity);
printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice);
printf("Total: %f\n", unitPrice);
input++;
break;

case 3:
unitPrice = (1.42 * quantity);
printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice);
printf("Total: %f\n", unitPrice);
input++;
break;

case 4:
unitPrice = (1000000.00 * quantity);
printf("%4.2f of item %d at %4.2f each = %4.2f\n\n\n", quantity, partID, unitPrice);
printf("Total: %f\n", unitPrice);
input++;
break;

default:
printf("Part cannot be found\n");
input++;
break;

}
}while (input < 1); // set this way otherwise it's an infinite loop


}

Link to comment
Share on other sites

  • 0

You have several problems with this iteration of your code. I'm actually a little surprised it doesn't seg. fault all the time. (It only seg. faults about 1 in every 5 times I run it.) The printf() statements in your switch case have four conversions specified but you only pass three variables to them. That means printf() is reading garbage data for the fourth conversion, which could result in either unspecified behavior or a seg. fault. One possible solution is to set unitPrice to the actual price per unit and calculate the total in your printf() statement.

unitPrice = 1.42;
printf("%4.2f of item %d at %4.2f each = %4.2f\n", quantity, partID, unitPrice, unitPrice * quantity);

Another problem is your scanf() of quantity. You are telling scanf() to interpret quantity as an integer while the variable itself is declared as a floating point. Because data storage is handled much differently for those variable types, the data your user enters for the quantity is not the same as the value stored in your variable. To correct this problem you either need to make quantity an int or scanf() "%f" instead of "%d". Based on your use of quantity I think the first solution is the better choice.

int quantity;

scanf("%d", &quantity);

Additionally, you could make your program more succinct by removing your do {} while() loop and placing the identical printf() statements outside of your switch case.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    unsigned int partID;
    unsigned int quantity;
    
    double unitPrice;
    double total;
    
    enter_part_id:
    printf("Please enter partID: ");
    scanf("%u", &partID);
    
    printf("Please enter quantity: ");
    scanf("%u", &quantity);
    
    switch(partID)
    {
        case 0:   
            unitPrice = 1.30;
            break;
        case 1:
            unitPrice = 0.99;
            break;
        case 2:
            unitPrice = 2.50;
            break;
        case 3:
            unitPrice = 1.42;
            break;
        case 4:
            unitPrice = 1000000.00;
            break;
        default:
            printf("Part cannot be found\n");
            goto enter_part_id;
    }
    
    total = unitPrice * quantity;
    
    printf("%u of item %u at %4.2f each = %4.2f\n", quantity, partID, unitPrice, total);
    
    return 0;
}
Link to comment
Share on other sites

  • 0

Orangekillerx,

 

is there a way to do that but without the array caller?  I haven't used them yet and we aren't supposed to...but I was just curious if there was another way.  I thank you for all of your help!  You have no idea how frustrated I was getting with this issue. 

Link to comment
Share on other sites

  • 0
is there a way to do that but without the array caller?  I haven't used them yet and we aren't supposed to...but I was just curious if there was another way.  I thank you for all of your help!  You have no idea how frustrated I was getting with this issue. 

 

Array caller? I'm assuming you mean the goto statement. Some programmers believe that goto is nothing but trouble and should not be used under any circumstances. Obviously I'm not of that persuasion, but I do believe there are very few instances where goto is the proper construct to use. I only used it in this case because it made the logic simpler. You could easily accomplish the same thing using a loop, but using unitPrice as your sentinel (since it is being set anyway) rather than another variable like you had before.

 

My intention was to give you an example, not for you to use my code verbatim. That said, you have been very good about asking intelligent questions and showing real effort to understand C, so I will give you the modifications you requested in good faith.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    unsigned int partID;
    unsigned int quantity;
    
    double unitPrice = 0.0;
    double total;
    
    /* Floating point numbers in C are not precise. You will encounter "odd"
     * bugs by attempting to compare a floating point number directly. For
     * example, if you set unitPrice = 1.30 then ask if(unitPrice == 1.30) the
     * comparison will most likely fail. unitPrice will be very close to 1.30,
     * just not exactly. Learn this lesson early and it will save you a lot of
     * grief later.
     */
    while(unitPrice < 1.0)
    {
        printf("Please enter partID: ");
        scanf("%u", &partID);
        
        printf("Please enter quantity: ");
        scanf("%u", &quantity);
        
        switch(partID)
        {
            case 0:   
                unitPrice = 1.30;
                break;
            case 1:
                unitPrice = 0.99;
                break;
            case 2:
                unitPrice = 2.50;
                break;
            case 3:
                unitPrice = 1.42;
                break;
            case 4:
                unitPrice = 1000000.00;
                break;
            default:
                printf("Part cannot be found\n");
                break;
        }
    }
    
    total = unitPrice * quantity;
    
    printf("%u of item %u at %4.2f each = %4.2f\n", quantity, partID, unitPrice, total);
    
    return 0;
}
Link to comment
Share on other sites

  • 0

actually i was referring to that and int main(int argc, char * argv[]) because from what i read that calls an array in main.  maybe what i read was wrong, but that goto statement has me pausing. 

Link to comment
Share on other sites

  • 0

Actually no, the way I declared main() is proper syntax according the C standard; anything else is a non-standard deviation. The two parameters are related to the command-line arguments passed to your program, and the operating system collects that information whether you declare them in your C code or not. argv is a two-dimensional array (technically a pointer to that array, but that detail is a subtlety of C that you don't need to worry about right now) containing the arguments as they were passed to your program. argc tells you how many arguments there are.

 

For example, the following program takes several command line arguments. It uses a for loop to read each element of the array (which each correspond to a single argument passed to the program) and sets flags (bitwise) to indicate their presence. It then uses a series of if statements to check these flags and act on them. This program is a trivial example, but I think it succinctly demonstrates a very common real-world use of the parameters taken by main().

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

/* Startup flags */
#define SF_NONE     0  /* No flags are present */
#define SF_COUNT    1  /* Count to 100 */
#define SF_QUIET    2  /* Be less verbose than normal */
#define SF_HELP     4  /* Display our help information */
#define SF_ERROR    8  /* Get us out of here, Mr. Scott! */

int main(int argc, char * argv[])
{
    unsigned int flags = SF_NONE;
    
    for(int i = 1; i < argc; i++)
    {
        switch(argv[i][0])
        {
            case '-':
                switch( argv[i][1] )
                {
                    case '-':
                        if(strcmp("--count", argv[i]) == 0) flags |= SF_COUNT;
                        else if(strcmp("--quiet", argv[i]) == 0) flags |= SF_QUIET;
                        else if(strcmp("--help", argv[i]) == 0) flags |= SF_HELP;
                        else flags |= SF_ERROR;
                        break;
                    case 'c':
                        flags |= SF_COUNT;
                        break;
                    case 'q':
                        flags |= SF_QUIET;
                        break;
                    default:
                        flags |= SF_ERROR;
                        break;
                }
                break;
            default:
                flags |= SF_ERROR;
                break;
        }
        
        if(flags & SF_ERROR) break;
    }
    
    if(flags & SF_ERROR)
    {
        fprintf(stderr, "invalid syntax\n");
        fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
        return 1;
    }
    
    if(flags & SF_HELP)
    {
        printf("Usage: %s [OPTION]\n\n", argv[0]);
        printf("Process arguments and do something with them.\n\n");
        printf("  -c, --count              count from 1 to 100\n");
        printf("  -q, --quiet              be less verbose than normal\n");
        printf("      --help               display this help information\n\n");
    }
    
    if(flags & SF_COUNT)
    {
        if(!(flags & SF_QUIET)) printf("Counting from 1 to 100 ...\n");
        for(int i = 1; i <= 100; i++) if(!(flags & SF_QUIET)) printf("%d ", i);
        if(!(flags & SF_QUIET)) printf("\n");
    }
    
    if(flags == SF_NONE)
    {
        printf("no flags\n");
    }
    
    return 0;
}

Examples of the output of this program when invoked with various arguments are below.

$ ./argtest 
no flags
$ ./argtest -h
invalid syntax
Try './argtest --help' for more information.
$ ./argtest --help
Usage: ./argtest [OPTION]

Process arguments and do something with them.

  -c, --count              count from 1 to 100
  -q, --quiet              be less verbose than normal
      --help               display this help information

$ ./argtest --count
Counting from 1 to 100 ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 
$ ./argtest --count --quiet
$ ./argtest -c
Counting from 1 to 100 ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 
$ ./argtest -c -q
Link to comment
Share on other sites

  • 0

actually i was referring to that and int main(int argc, char * argv[]) because from what i read that calls an array in main.  maybe what i read was wrong, but that goto statement has me pausing. 

If I may add to what xorangekiller said, the return type of main is int. void is accepted by Visual C++ but it's incorrect; it will not compile on other compilers. Stick to int main().

 

The "int argc, char** argv" part is the program arguments. If you're not interested in the program arguments, you can omit them and just write int main() or int main(void). This has no effect on the execution of the program. It does not "call an array" (which is nonsensical - only functions can be called, arrays are just data).

Link to comment
Share on other sites

  • 0

Orangekillerx and Asik, thank you for explaining that to me.  Apparently what I was reading was just some guy full of hot air trying to explain something he knew nothing about.  I really appreciate you taking the time to explain this to me in terms that I'm grasping, slowly, but hey it's still progress!  Thank you again!

Link to comment
Share on other sites

  • 0

Okay one more question about this orangekillerx.  How does this work:

    enter_part_id:9

 

I've never seen this done before.  Could you please explain it to me?

Link to comment
Share on other sites

  • 0

Okay one more question about this orangekillerx.  How does this work:

    enter_part_id:9

 

I've never seen this done before.  Could you please explain it to me?

 

If you are referring to the iteration of your code I posted yesterday in post #10, enter_part_id: is a label. It literally does nothing by itself. In fact, the compiler doesn't translate it into any machine instructions at all, even a nop (no operation - waste a CPU cycle). However, it marks a place in the code that you can return to later in the function (and this is an important limitation - only within the same function) using a goto statement. So in the program in the aforementioned post, the goto statement on line 38 references the label on line 12. So in the default "part cannot be found" case of the partID switch, the goto statement sets the program counter back to line 12 (rather than incrementing it to line 39 like would have happened otherwise) so the user is prompted to enter a partID again.

Link to comment
Share on other sites

  • 0

Oh okay!  Thanks!  That makes a lot of sense.  I thought that might be something it was doing, but I wanted to get confirmation first before I tried messing around with it. 

Link to comment
Share on other sites

  • 0

Okay, so I tried to do something similar to what you did orangekillerx .  I tried modifying what you had done and tried to use it for answers to a monty python skit from "the holy hand grenade".  here is what I have:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
	unsigned int num;
	int monty_python;
	monty_python = 0;

	enter_monty_python:	
	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n", num);
	scanf("%d", &num);

	switch(monty_python)
		{
	case 1:
		printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", monty_python);
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", monty_python);
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", monty_python);
		break;
	case 4:
		printf("Four thou shall not count, neither count til two.\n", monty_python);
		break;
	case 5:
		printf("Five is RIGHT OUT.\n", monty_python);
		break;
		case 0:
		printf("Amen\n", num);
		break;
	default:
		printf("That is not a correct choice, please try again\n");
		break;
		goto monty_python;
	}
	return 0;


}

However.  Its telling me that I am referencing monty_python but it is not defined.  before I did that I tried:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
	int num = 0;
	switch(num)
	do{
	

	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n");

	case 1:
		printf("One shall not be the number thou shalt count, and the number of they counting shall be three.\n");
		num++;
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n");
		num++;
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n");
		num++;
		break;
	case 4:
		printf("Four thou shalt not count, neither count til two.\n");
		num++;
		break;
	case 5:
		printf("Five is RIGHT OUT.\n");
		num++;
		break;
	default:
		printf("That is not a correct choice, please try again\n");
		num++;
		break;
	case 0:
		printf("Amen\n");
		num++;
		break;
	}

	while (num != 0);
	


}

neither of which are working.  where am I messing up?  I've tried it more than these two ways and I'm just not getting this at all.  I figured that if I kept trying different ways to do the same things that eventually I'd pick it up, but these switches are just not sinking in.   On the second one I'm only getting the output of Amen, which means its going directly to 0 and exiting. 

Link to comment
Share on other sites

  • 0

I feel like I have just given you a dangerous weapon, and I would be remiss if I did not mention the reason that many professors (and even many C textbooks) do not discuss goto at all. The goto statement is considered by some to be a relic of a by-gone age. It is a critical construct in most assembly languages, but is largely obviated by more specialized - not to mention less error-prone - constructs in high-level languages like C. For example, the way I re-wrote the program from post #10 in post #12 would most likely be considered the "correct" way to write that program by goto detractors. The reason the goto statement has acquired such a venomous reputation that many do not teach it and even more strongly believe that it should be avoided at all costs, is because programmers have a tendency to abuse it to write spaghetti code. Although it is occasionally the best way to structure your control logic, it is generally a good idea to avoid using goto unless the alternatives would make your code significantly more complicated.

Link to comment
Share on other sites

  • 0

LOL!  Okay, forget the first set of my previous post and ill work on the second.  My switch loop is going directly to the 0 (Amen) phase and exiting.  What did I miss because I'm just not seeing it. 

Link to comment
Share on other sites

  • 0

Wait!  I went through and tried this...but its still ignoring everything but the case 0.  :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
	int num = 0;
	switch(num)
	do{
	

	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n", num);
	scanf("%d", &num);

	case 1:
		printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", num);
		num++;
		break;
	case 2:
		printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", num);
		num++;
		break;
	case 3:
		printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", num);
		num++;
		break;
	case 4:
		printf("Four thou shall not count, neither count til two.\n", num);
		num++;
		break;
	case 5:
		printf("Five is RIGHT OUT.\n", num);
		num++;
		break;
	default:
		printf("That is not a correct choice, please try again\n", num);
		num++;
		break;
	case 0:
		printf("Amen\n", num);
		num++;
		break;
	}
		while (num != 0);
}

post-505691-0-81362700-1380672525.png

Link to comment
Share on other sites

  • 0

You have several mistakes in this new program. To be honest, I'm a little surprised that it compiles. Your biggest problem is that your switch statement should be inside of your loop, not outside. In the interest of reducing redundant code and simplifying your logic, you should probably put num++ outside of your switch case rather than repeating it in every case statement. Additionally, why are you incrementing num at all? Since you require your user to enter a new number (overwriting num) before it is used again, incrementing it seems wholly useless.

Link to comment
Share on other sites

  • 0

I'm not even sure what this program is supposed to mean.  :/ Stop, think and go through this logically.

 

Start by writing it out in whatever pseudo-code:

Initially set user input to something other than 0
WHILE user input is not 0
     get user input
     SWITCH on user input:
         | 0 -> print "Amen"
         | 1 -> print "One shall be..."
         (...)
         | default -> print "that is not a valid entry"

Can you run this mentally and see why it works? If so it should be obvious where your implementation is wrong.

Link to comment
Share on other sites

  • 0

Okay, after much thought and concentration, I FIXED IT!!! BOOYEAH!  (sorry about the excitement, but I'm so happy!!!)

 

Here is what I did:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
void main()

{

	unsigned int num;
	printf("What number doest thou wish, between 1 and 5 (0 to exit):\n");
	scanf("%d", &num);

	switch(num)

{
	case 1:
	printf("One shall be the number thou shalt count, and the number of they counting shall be three.\n", num);
	break;

	case 2:
	printf("Neither thou count Two, 'cepting that thou then proceed to Three.\n", num);
	break;

	case 3:
	printf("Three shall be the number thouh shalt count, and the number of thy counting shall be three.\n", num);
	break;

	case 4:
	printf("Four thou shall not count, neither count til two.\n", num);
	break;

	case 5:
	printf("Five is RIGHT OUT.\n", num);
	break;

	case 0:
	printf("Amen\n", num);
	break;

	default:
	printf("That is not a correct choice, please try again\n");
	break;
}

}

Everything works flawlessly thanks to your help and a friend of mine helping me figure out where I went wrong.  Again, thank you for all of your help and patience with me.  This has not been easy for me to go from pure user with no experience whatsoever to writing code.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.