Thread: Errors in Dev C++ with else statment

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    1

    Errors in Dev C++ with else statment

    Hi! I'm a beginner in programming and I am in a principles of programming class. The requirements are that I use Dev C++ to compile and run the programming problems for the homework. This is the problem I am working on with the pseudocode:

    The Daily Trumpet newspaper accepts classified advertisements in 15 categories such as Apartments for Rent and Pets for Sale.
    Develop the logic for a program that accepts classified advertising data, including category code (an integer 1 through 15) and number of words in the ad. Store these values in parallel arrays. Then sort the arrays so that records are in ascending order by category. The output lists each category, the number of ads in each category, and the total number of words in the ads in each category.


    Code:
    Code:
    #include <cstdlib>
    #include <iostream>
     
    using namespace std;
    int main(int argc, char *argv[])
    {
        int MAXADS=100;
        int adcatcode [MAXADS]; 
        int adwords [MAXADS];
        int curCode;
        int numads;
        int i;
        int j;
        int k;
        int subtotal;
        int temp;
       
        cout<< "Please enter the number of ads: ";
        cin>> numads;
       
        if((numads > 0) && (numads <= MAXADS));
           for (i=0; numads - 1;)
           {
           cout<< "Please enter advertisement Category Code (1 - 15):";
           cin>> adcatcode[i];
           cout<< "Please enter number of words for the advertisement:";
           cin>> adwords[i];
           }
           for (i = 0; numads - 2;)
            {
                  for (j = 0; numads - 2;)
                  {
                      if (adcatcode[j] > adcatcode [j+1]);
                      {
                                       temp = adcatcode[j];
                                       adcatcode[j] = adcatcode[j+1];
                                       adcatcode[j+1] = temp;
                                       temp = adwords[j];
                                       adwords [j] = adwords[j+1];
                                       adwords[j+1] = temp;
                                       }
                                       }
                                       }
           cout<< "Total Word Counts Sorted By Category Code";
        cout<< "=========================================";
                      k = 0;
                      while (k <= numads -1);
                      {
                            subtotal = 0;
                            curCode = adcatcode[k];
                    while ((curCode = adcatcode[k]) && (k <= numads-1));
                            {
                                  subtotal = subtotal + adwords[k];
                                  k = k + 1;
                                  }
       cout<< "Category: ",adcatcode[k-1], " ","Word Count: ",subtotal;
                                  }  
                      else
                       {
                           cout<< "Number adds requested less than 1 or is too large; ad limit is ",  MAXADS;
                          }
                          }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Everything compiles fine until I add the else statement. Also if I remove the else statement it compiles and runs but keeps me in the loop asking for the advertisement category code and the number of words for the advertisement. The errors I am getting once I add the else statement are :

    PLD Ch8 7a.cpp: In function `int main(int, char**)':
    PLD Ch8 7a.cpp:59: error: expected primary-expression before "else"

    PLD Ch8 7a.cpp:59: error: expected `;' before "else"

    PLD Ch8 7a.cpp: At global scope:
    PLD Ch8 7a.cpp:64: error: expected constructor, destructor, or type conversion before '(' token
    PLD Ch8 7a.cpp:64: error: expected `,' or `;' before '(' token
    PLD Ch8 7a.cpp:65: error: expected unqualified-id before "return"
    PLD Ch8 7a.cpp:65: error: expected `,' or `;' before "return"
    PLD Ch8 7a.cpp:66: error: expected declaration before '}' token

    make.exe: *** ["PLD Ch8 7a.o"] Error 1

    Execution terminated

    Any help is very much appreciated!! And sorry it is so long I just wanted to give as much info as possible.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your main problem is that if statements (the same applies to any loop control constructor such as while and for) shall not end with a semicolon. That basically means an "empty" body. Because the body is empty, you are putting some contents between the the if and the else, which is not allowed. Remove the semicolon.

    There are also other glaring problems in your code. Here are a few.
    First off, you should indent your code properly:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    int main(int argc, char *argv[])
    {
    	int MAXADS = 100;
    	int adcatcode[MAXADS];
    	int adwords[MAXADS];
    	int curCode;
    	int numads;
    	int i;
    	int j;
    	int k;
    	int subtotal;
    	int temp;
    
    	cout << "Please enter the number of ads: ";
    	cin >> numads;
    
    	if ((numads > 0) && (numads <= MAXADS));
    		for (i = 0; numads - 1;)
    		{
    			cout << "Please enter advertisement Category Code (1 - 15):";
    			cin >> adcatcode[i];
    			cout << "Please enter number of words for the advertisement:";
    			cin >> adwords[i];
    		}
    	for (i = 0; numads - 2;)
    	{
    		for (j = 0; numads - 2;)
    		{
    			if (adcatcode[j] > adcatcode[j + 1])
    			{
    				temp = adcatcode[j];
    				adcatcode[j] = adcatcode[j + 1];
    				adcatcode[j + 1] = temp;
    				temp = adwords[j];
    				adwords[j] = adwords[j + 1];
    				adwords[j + 1] = temp;
    			}
    		}
    	}
    	cout << "Total Word Counts Sorted By Category Code";
    	cout << "=========================================";
    	k = 0;
    	while (k <= numads - 1)
    	{
    		subtotal = 0;
    		curCode = adcatcode[k];
    		while ((curCode = adcatcode[k]) && (k <= numads - 1))
    		{
    			subtotal = subtotal + adwords[k];
    			k = k + 1;
    		}
    		cout << "Category: ", adcatcode[k - 1], " ", "Word Count: ", subtotal;
    	}
    	else
    	{
    		cout << "Number adds requested less than 1 or is too large; ad limit is ", MAXADS;
    	}
    }
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    Here we see that you've made some weird errors such that if and elses don't add up and the number of braces do not add up either. Put braces around your if and loops unless they're only one line long.

    >>int adcatcode[MAXADS];
    This would be fine if MAXADS was a constant expression, but it is not. It is an int, and therefore, this is not valid C++.
    You should declare MAXADS as
    const int MAXADS = some number;
    or
    constexpr int MAXADS = some number; (if your compiler supports it)

    >>int adcatcode[MAXADS];
    Prefer to use std::array:
    std::array<int, MAXADS> adcatcode;
    Also, if using std::array, prefer to use .at() instead of the index operator because there is a better chance you will detect an out-of-bounds read/write using that than the index operator which may silently erroneously succeed (not guaranteed to crash, but using .at() is), e.g.:
    adcatcode.at(i) = ...;

    >>if ((numads > 0) && (numads <= MAXADS));
    Prefer testing for negative conditions instead. If the assumption doesn't hold, then print an error and exit. This means you don't need big nested blocks which results in clearer code.

    >>for (i = 0; numads - 1;)
    This is a clear example of where you should declare variables near first use. The loop variable i should be declared inside the for loop and not at the beginning of the function:
    for (int i = 0; numads - 1;)

    You also forgot to increment i - because in C++, loop variables do not automatically increment themselves, so you'll be running the loop over and over infinitely:
    for (int i = 0; numads - 1; i++)

    >> temp = adcatcode[j];
    >> adcatcode[j] = adcatcode[j + 1];
    >> adcatcode[j + 1] = temp;
    This looks like a swap. C++ has a function for that. It's called std::swap. So:

    using std::swap; // If not using "namespace std;"
    swap(adcatcode[j], adcatcode[j + 1]);

    >>cout << "Category: ", adcatcode[k - 1], " ", "Word Count: ", subtotal;
    Will not do what you want it to do. To print multiple things in the same statement, use the stream operator:
    cout << "Category: " << adcatcode[k - 1] << " " << "Word Count: " << subtotal;
    Last edited by Elysia; 11-07-2014 at 08:21 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  2. If statment qustion
    By Yizi in forum C Programming
    Replies: 5
    Last Post: 11-21-2009, 04:53 AM
  3. C++ If statment question
    By TehClutchKiller in forum C++ Programming
    Replies: 17
    Last Post: 05-28-2008, 07:33 AM
  4. Weird Statment...
    By Devil Panther in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 03:55 PM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM

Tags for this Thread