I'm in a rough situation, i'm currently re-reading all of my textbooks to assist me with this work, however I am really in a rough spot being time limited I really want to get somewhere from what I have (and fix what I have).

What I need to correct/add.
-I need case#3 to calculate properly, I'm not getting any calculation working.
I also cannot use a pow() function, so i used this, I cannot get it working so I will post it apart from the working code. Hopefully this is able to be incorporated.

counter = 1;
sum = 1;
while(counter <= exponent){
sum = sum*base;
counter++;
}
printf("The Sum is %d\n",sum);

-I need the code to "reshow" the menu instead of breaking/closing the code (only way to exit should be case#4 and nothing else.


-I used scanf, which i've found out is not allowed, and I wish to use getnum(), getch(), or any alternative to scanf, fscanf.

I'm very frustrated and overwhelmed with reading/doing this/time limitations/other classes. I need some assistance, as once I have these steps...hopefully I will be able to complete it. all of the following is code I have made thus far.

I'm VERY great-full if you are able to help me with this, as I have spent HOURS trying to do this on my own.

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

int main(void)
{
	
    int selection = 0;
    double base = 1;
    double exponent = 1;
    int sum = 1; 

    printf("\nPower Menu:\n");
    printf("  1. Change Base\n");
    printf("  2. Change Exponent\n");
    printf("  3. Display base raised to exponent\n");
    printf("  4. Exit Program\n"); 
    printf("Option:");

    scanf("%d", &selection);

    switch(selection)
    {
        case 1:
            printf("Enter Base :");
	    printf("Base = %d\n", base, scanf("%d", &base)); 
            break; 
        case 2:
            printf("Enter Exponent :");
	    printf("Exponent = %d\n", exponent, scanf("%d", &exponent)); 
            break;
        case 3:
	    printf("The Sum is %d\n", pow(base,exponent));
            break;
        case 4:
            printf("Exiting Program\n");
            return 0;
        default:
            printf("Invalid Choice\n");
	    break;
    }
    
    return 0;
}

Edited 6 Years Ago by Mittwaffen: n/a

Has your coursework covered writing your own functions, yet? Given the particular restrictions, and the usual solutions to them both, I would expect that this is meant as an exercise in function writing - more specifically, in writing recursive functions, as both exponentiation and converting a decimal string to an integer have simple recursive solutions.

As for the menu, the simple solution is to have an indefinite loop around the code which prints the menu and performs the calculations:

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

void print_menu();
double exponentiate(double radix, int base);
int get_int();

int main(void)
{
    
    int selection = 0;
    double base = 1;
    double exponent = 1;
    int sum = 1;
    int repeat = 1;

    while (repeat)
    {
        print_menu();

        selection = get_int();

        switch(selection)
        {
            case 1:
                printf("Enter Base :");
                base = get_int();
                printf("Base = %d\n", base); 
                break; 
            case 2:
                printf("Enter Exponent :");
                exponent = get_int();
                printf("Exponent = %d\n", exponent); 
                break;
            case 3:
                printf("The Sum is %d\n", exponentiate(base,exponent));
                break;
            case 4:
                printf("Exiting Program\n");
                repeat = 0;
                break;
            default:
                printf("Invalid Choice\n");
                break;
        }
    }
    
    return 0;
}

void print_menu()
{    
    printf("\nPower Menu:\n");
    printf("  1. Change Base\n");
    printf("  2. Change Exponent\n");
    printf("  3. Display base raised to exponent\n");
    printf("  4. Exit Program\n"); 
    printf("Option:");
}

Writing exponentiate() should be trivial; you have most of it written already. Writing get_int() is rather more difficult, but you should try it yourself before getting more help. I will tell you that it is a recursive function (that is, one that calls itself), and involves using modulo (%), division, and multiplication.

Edited 6 Years Ago by Schol-R-LEA: n/a

This article has been dead for over six months. Start a new discussion instead.