Home > database >  How can I get a different statement printed to the user each loop iteration?
How can I get a different statement printed to the user each loop iteration?

Time:12-02

How can I make my code display different print statements to the user while in a for loop? The goal of the code is to solve for a unknown side of a right triangle knowing the other two sides.

My code works as intended, however there is no guide as to which side the user would be inputting a value in for. Is there any way I can have a print statement that displays which side the user will enter a value for in the for loop?

For example: during the first run through the loop the code will display "enter a value for side A" then the next run will display "enter a value for side B" then the last run will display "enter a value for side C".

#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdio.h>

float TriSideSolver(float side1, float side2, float side3, float* ptrA, float* ptrB, float* ptrC);
void main(void)
{
    float inputA, inputB, inputC; // needed variables
    int success;
    int i;
    float known[3]; 
    float A, B, C;
    printf("Input the known sides of the triangle, enter zero for the unknown side\n"); // prints instructions to user
    for (i = 0; i < 3; i  ) // for loop assigning values to the sides of the triangle.
    {
        scanf("%f", &known[i]);
    }
    A = known[0]; // assign inputs to variables
    B = known[1];
    C = known[2];

    success = TriSideSolver(A, B, C, &inputA, &inputB, &inputC); // call to use function.

    A = inputA; // assign new values to variables
    B = inputB;
    C = inputC;
    printf("These are the results:\n A= %f\n B= %f\n C= %f\n", A, B, C); // print values to the user 

}//end of main

float TriSideSolver(float side1, float side2, float side3, float* ptrA, float* ptrB, float* ptrC)
{ 
    if (side1 == 0)
    { // need to find side A
        *ptrA = sqrt((pow(side3, 2)) - (pow(side2, 2)));
        *ptrB = side2;
        *ptrC = side3; 
        return 1;
    }
    else if (side2 == 0)
    {// need to find side B
        *ptrB = sqrt((pow(side3, 2)) - (pow(side1, 2)));
        *ptrA = side1;
        *ptrC = side3;
        return 1;
    }
    else if (side3 == 0)
    {// need to find side C
        *ptrC = sqrt((pow(side1, 2))   (pow(side2, 2)));
        *ptrA = side1;
        *ptrB = side2;
        return 1;
    }
    else //if user inputs 3 sides
    {
        *ptrA = side1;
        *ptrB = side2;
        *ptrC = side3;
        return 1;
    }

}//end of function

CodePudding user response:

You can store names of the sides in an array of characters and print them in the right order in the loop.

A minimal example would be:

#include <stdio.h>

int main()
{
    float known[3];
    char side_names[] = {'A', 'B', 'C'};
    int i = 0;

    for (i = 0; i < 3; i  ) // for loop assigning values to the sides of the triangle.
    {
        printf("Input the length of side: %c\n", side_names[i]);
        scanf("%f", &known[i]);
    }
}

Here side_names stores characters representing each side, in the same order they are collected in the loop. Note that if you were to store strings, things would look a little different.

  • Related