counting and swapping numbers

Ok this is the end of a HW assignment in my c++ class. I included the pseudocode. The error i am having is somewhere at the bottom and the program wont compile fully. if anyone can point out what i did wrong i would appreciate it. Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  
// Start
//     Declarations
//         num MAXADS = 100
//         num adcatcode[MAXADS]
//         num adwords[MAXADS]
//         num curCode
//         num numads
//         num i
//         num j
//         num k
//         num subtotal
//         num temp
//     output "Please enter the number of ads: "
//     input numads
//     if ((numads > 0) and (numads <= MAXADS))
//         for i = 0 to numads - 1
//           output "Please enter Advertisement Category Code (1 - 15): "
//           input adcatcode[i]
//           output "Please enter number of words for the advertisement: "
//           input adwords[i]
//         endfor
//         for i = 0 to numads - 2
//           for j = 0 to 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
//             endif
//           endfor
//         endfor
//         output "Total Word Counts Sorted By Category Code"
//         output "========================================="
//         k = 0
//         while k <= numads - 1
//             subtotal = 0
//             curCode = adcatcode[k]
//             while ( (curCode = adcatcode[k]) and (k <= numads - 1) )
//                 subtotal = subtotal + adwords[k]
//                 k = k + 1
//             endwhile  
//             output "Category: ",adcatcode[k - 1], "  ","Word Count: ", subtotal
//         endwhile
//     else
//         output "Number adds requested less than 1 or is too large; ad limit is ",  MAXADS
//     endif
// Stop

#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; i < numads; numads--)
    {
    cout << " Please enter the Advertisement Category Code (1 - 15) : ";
    cin >> adcatcode[i];
    cout << " Please enter the number of words for the advertisement: ";
    cin >> adwords[i];    
    }

    for(i = 0; i < numads; numads - 2)
{
      for( j = 0; j < numads; 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" << endl;
             cout << "=========================================" << endl;
             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 << endl;
             }
                   else
                   {
                       cout << "Number adds requested less than 1 or is too large; ad limit is " << MAXADS;
                   }
    system("PAUSE");
    return EXIT_SUCCESS;
}
What do you mean "somewhere"? Does your compiler not report the line number and the error?
lol sorry yea it is in line 110. I believe it is something in or before the else statement but even after moving values around its not compiling
This else statement

1
2
3
4
                   else
                   {
                       cout << "Number adds requested less than 1 or is too large; ad limit is " << MAXADS;
                   }


has no corresponding if statement.

You should format the code that it will be easy readable.
i thought that in the pseudo code it showed that line 110's else statment corresponds with the if on line 85 and the while statments on line 99 or 103?
the output of this error sums it up:
1
2
test.cpp: In function ‘int main(int, char**)’:
test.cpp:59:20: error: ‘else’ without a previous ‘if


you cant just throw an else clause anywhere in the code and expect it to correspond with another if condition. It doesn't matter what your pseudo code says, as its just comments in the code, if you wrote it out like:
1
2
3
4
5
6
7
8
9
10
11
12
13
           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;
             }
           else
           {
               cout << "Number adds requested less than 1 or is too large; ad limit is " << MAXADS;
           }

it would work. You should organize your indentation to make it easier to read the code, the brackets are all over the place
Last edited on
the program will compile but the output is garbage, and it neither activates the else statement when i enter a integer higher than the supposed option numbers.
Try debugging the code to find the logical error you made.
tried debugging. didnt help
the program seems to just skip the last else statement and not change if i enter a category that is not within 1-15
I played with it some more and here is the completed answer for anyone's future reference


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    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; i < numads; i++)
     {
    cout << " Please enter the Advertisement Category Code (1 - 15) : ";
    cin >> adcatcode[i];
    cout << " Please enter the number of words for the advertisement: ";
    cin >> adwords[i];    
    }
        for(i = 0; i < numads; i ++)
        {
              for(j = 0; j < numads; j ++)
              {
           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" << endl;
             cout << "=========================================" << endl;
             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 << endl;   
              } 
                    
 
 
}
  else
{
cout << "Number adds requested less than 1 or is too large; ad limit is " << MAXADS << endl;
 }             
             
                
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.