none
"Only assignment, call, increment, decrement and new object can be used as a statement" error, PLEASE HELP!!! RRS feed

  • Question

  • Hello, I am having an issue with this program I am writing in C# for a course to calculate the average worker salaries based on education type, and there seems to be a problem with the if/else statements that I can't seem to figure out. I am getting the error "only assignment, call, increment, decrement and new object can be used as a statement".
    using System;
    public static class Assign2
    {
        public static void Main()
        {
            char edType = 'A';
            double salaryData = 1.0, universityAvg, collegeAvg, highschoolAvg, universityTotal, collegeTotal, highschoolTotal;
            int universityCount, collegeCount, highschoolCount;
    
            while (char.ToUpper(edType) != 'Q')
            {
                Console.Write(@"Enter the Education Type of the worker (U for university degrees, C for college degrees,
                H for high school diplomas, or Q to quit): ");
                edType = Convert.ToChar(Console.ReadLine());
    
                Console.Write("Enter their salary: ");
                salaryData = Convert.ToDouble(Console.ReadLine());
    
                while (salaryData < 0)
                {
                    Console.Write("That is an invalid entry. Please try again.");
    
                    Console.Write("Enter their salary: ");
                    salaryData = Convert.ToDouble(Console.ReadLine());
                }
    
                if (edType == 'U')
                    universityCount++;
                    universityTotal += salaryData;
                if (edType == 'C')
                    collegeCount++;
                    collegeTotal += salaryData;
                if (edType == 'H')
                    highschoolCount++;
                    highschoolTotal += salaryData;
                else
                    Console.Write("That is an invalid entry. Please try again.");
    
                salaryData = -1.0;
            }
    
            universityAvg = universityTotal / universityCount;
            collegeAvg = collegeTotal / collegeCount;
            highschoolAvg = highschoolTotal / highschoolCount;
        }
    }
    P.S. I haven't finished the program, but it shouldn't have any effect on the error.
    Thursday, February 8, 2018 5:19 AM

Answers

  • Your code does not compile on VS2012 desktop edition. The reason is different from what you state

                if (edType == 'U')
                    universityCount++;
                    universityTotal += salaryData;
                if (edType == 'C')
                    collegeCount++;
                    collegeTotal += salaryData;
                if (edType == 'H')
                    highschoolCount++;
                    highschoolTotal += salaryData;
                else
                    Console.Write("That is an invalid entry. Please try again.");
    
                salaryData = -1.0;

    Your if statements are probably missing { and }; currently e.g. the first condition will only affect universityCount and universityTotal will be incremented regardless of the condition.For that reason, your else does not have a matching if.

    Advise for this: always use {} for conditional blocks; it prevents a lot of unexpected behaviour when your code runs.

    Next you probably also want to use else if instead of if. Else your else only applies to the last if and when you enter e.g. 'U' you will still get the message about the invalid entry.

    Once you have that solved, the last part of your code will complain about uninitialized variables. You will need to initialise those variables when you declare them.

    • Proposed as answer by Ante Meridian Thursday, February 8, 2018 5:43 AM
    • Marked as answer by blackbearox Thursday, February 8, 2018 5:52 AM
    Thursday, February 8, 2018 5:34 AM