-2

So basically I was creating a program to ask the user how many times they wanted to test the program.But I couldnt figure out the problem with my for loop. So:

  1. If the user wants to test the program 3 times, it should ask for the value of a 3 times only and it should quit after.

This is my code as follows:

#include <stdio.h>

int main()
{


    int test;
    printf("How many times do you want to test the program?");
    scanf("%d", &test);
    test = 0; // Reinitializing the test from 0
    for (test=0; test=>1; test++) //I cant figure out whats going on with the for loop.
    {
        printf("Enter the value of a: \n");
        scanf("%d", &test);
        ;

    }
    return 0;
}

The output should be: "How many times do you want to test the program": 3 Enter the value of a : any numerical value Enter the value of a: any numerical value Enter the value of a: any numerical value (exit)

user0042
  • 7,917
  • 3
  • 24
  • 39
Dev_109
  • 55
  • 1
  • 1
  • 7

1 Answers1

1

In this section of your code:

scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++) 

First, memory owned by test is populated with a value entered by user. (this is OK)
Next, you nullify that new value in memory by setting test to zero. (this is not OK)
finally the construction of your loop statement is incorrect.

In a correct version of your for loop, test should be a value used as a limit against which an index is tested as that index is incremented across a range of values, for example, from 0 to some positive value.

You probably intended:

scanf("%d", &test);
//test = 0; // Reinitializing the test from 0 (leave this out)
for(int i = 0; i <= test; i++)
{
    ...  

Where a separate index value (i) is incremented and tested against the limit test.

ryyker
  • 22,849
  • 3
  • 43
  • 87