4

So the idea is to ask the user for each element of the array, but after an input is given for the first question (where it asks for the amount of elements), nothing happens. Can't figure out why.

#include <iostream>

int main()
{
        int numGrades;
        tryAgain:
        std::cout << "Enter number of grades" << std::endl;
        std::cin >> numGrades;

            if (numGrades > 30)
                {
                std::cout << "Please enter a valid number of grades" << std::endl;
                goto tryAgain;
                }


        int grades[numGrades - 1];
        int gradeCount = 0;
        while (gradeCount < numGrades);
            {
            std::cout << "Enter grade number" << gradeCount + 1 << ":";
            std::cin >> grades[gradeCount];

            ++ gradeCount;
            }   

        std::cout << grades;
        return 0;   
}
Ilya
  • 4,583
  • 4
  • 26
  • 51
  • Use a debugger. – Amit Sep 19 '16 at 04:44
  • 10
    Looks like you have a semicolon after the while loop ;) – Passersby Sep 19 '16 at 04:47
  • 1
    The semicolon at the end of while() might be a culprit. Check [this question for more details](http://programmers.stackexchange.com/questions/202734/putting-semicolons-after-while-and-if-statements-in-c) – hssay Sep 19 '16 at 04:48
  • You could start by printing out the value of `numGrades` just before the loop to see if it's what you expected. It probably is working, but you need to call `std::flush` when you prompt for a grade. As a side-note: because you don't have any error checking, this code will freak out if you enter something non-numeric. Also VLAs are not part of the C++ standard. – paddy Sep 19 '16 at 04:49
  • 2
    while (gradeCount < numGrades); remove the semicolon – MD. Nazmul Kibria Sep 19 '16 at 04:50
  • 2
    `int grades[numGrades - 1];` is not allowed in standard C++ . Instead use `vector grades(numGrades);` and also check that the number is not negative before doing this. – M.M Sep 19 '16 at 04:54

3 Answers3

6

The constuction while (true); means while (true) {} (i.e. infinite loop).

So, when you write

while (gradeCount < numGrades);
{
  // ...
}

you have the following:

while (gradeCount < numGrades)
{
}

{
  // ...
}

Second block will never be executed if gradeCount < numGrades.

Ilya
  • 4,583
  • 4
  • 26
  • 51
1

You are using

while (gradeCount < numGrades);

with a semi-colon (;) at the end of this line so the next line will not exectue because the condition is always true as there is no increment or decrement in the respective variables.

In short just remove the (;)

while (gradeCount < numGrades)
Ahsan
  • 412
  • 5
  • 17
0

Please see this code, there were few problems. One is semicolon on while loop & another one is printing grades & memory allocation of the grades. Memory static allocation must need a constant value. Here a dynamic allocation is added as the grades number is not fixed or constant... Here is the code:

#include <iostream>

int main()
{
        int numGrades;
        tryAgain:
        std::cout << "Enter number of grades" << std::endl;
        std::cin >> numGrades;

            if (numGrades > 30)
                {
                std::cout << "Please enter a valid number of grades" << std::endl;
                goto tryAgain;
                }



        int *grades = (int *)malloc(numGrades * sizeof(int)); //allocating dynamic memory

        int gradeCount = 0;
        while (gradeCount < numGrades)
            {
            std::cout << "Enter grade number" << gradeCount + 1 << ":";
            std::cin >> grades[gradeCount];

            ++ gradeCount;
            }   


        for(int i =0;i<numGrades;i++)
        {
            std::cout << grades[i] << std::endl;
        }

        free(grades);//releasing memory

        return 0;   
}
MD. Nazmul Kibria
  • 1,080
  • 10
  • 21
  • I'd recommend you to read about "variable length arrays" (may be here: http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c ). We don't know what the compiler uses CptJohnMiller74, but it is possible that this code is compilible on his side. – Ilya Sep 19 '16 at 05:01
  • thanks, yes it may vary from compiler to compiler ... – MD. Nazmul Kibria Sep 19 '16 at 05:05