Need help with Code

Hi, I'm writing a program that will test integer values to find out its factors and whether it is prime. It will run a batch file with 3 integer values as data within the file. Using cin >>, the program will pull the first two values that will serve as the range of integer values to be tested. The third integer value will be used as the specified number of factors we are testing for.


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
// Input: Data from a batch file containing 3 integer values. The first and
// second values represent the range of values to be tested. The first value
// will be less than or equal to the second value. The third integer value
// represents the number of factors being tested for within the range of 
// values INCLUDING the first two integer values within the batch file.
// Output: A list of numbers in the range that have the number of factors
// specified by the third integer, the number of numbers with the specified
// factor count, the number of prime factors found within the range and finally
// the average of the prime numbers found.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{

  int first;          // First integer value within range of tested values
  int last;           // Last integer value within range of tested values
  int numoffactors;   // Number of specified factors being tested for
  int counter1;       // This is the first loop control variable
  int counter2;       // This is the second loop control variable
  int counter3;       // This is the variable to store the number of factors
                      // that the given number has
  int counter4 = 0;   // This is the variable to store the number of integer
                      // values that have the specified factors
  int prime = 0;      // This the variable to store number of prime numbers
  int primavg;        // This is the average of all prime factors
  int primesum;       // This is the sum of all prime factors

  cout << "\n";
  cout << "Jesse Yapi - Assignment #6 - Section #1003" << endl;
  cout << "\n";

  cin >> first >> last >> numoffactors;

  cout << "Numbers with at least " << numoffactors << " between " << first;
  cout << " and " << last << ":" << endl;
  for (counter1 = first, counter1 <= last, counter1 = counter1+1)
    {
      counter3 = 0;
      for (counter2 = 1, counter2 < counter1, counter2++)
        {
          switch (counter1 % counter2)
            {
            case 0:
            counter3++;
            if (counter3 >= numoffactors)
            counter4++;
            cout << counter1 << endl;
            break;
            }
        }
          if (counter3 == 2)
            prime++;
          primesum = primesum + counter1;
            else 
          cout << endl;
    }     
  cout << counter4 << " numbers with at least " << numoffactors
       << " factors found" << endl;
  cout << "Prime factors found: " << prime << endl;
  cout << fixed << showpoint << setprecision(4);
  cout << "Average of primes: " << primesum/prime << endl;

  return 0;

}


I'm very new to this but every time I try to compile my program I get these errors:

In function âint main()â:
error: expected â;â before â)â token
error: expected primary-expression at end of input
error: expected â;â at end of input
error: expected primary-expression at end of input
error: expected â)â at end of input
error: expected statement at end of input
error: expected â}â at end of input


Also, I'm sure there is a much more efficient method to doing this. I'd love your insight. PLEASE HELP ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Last edited on
Check the syntax around that if statement.
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
// Input: Data from a batch file containing 3 integer values. The first and
// second values represent the range of values to be tested. The first value
// will be less than or equal to the second value. The third integer value
// represents the number of factors being tested for within the range of
// values INCLUDING the first two integer values within the batch file.
// Output: A list of numbers in the range that have the number of factors
// specified by the third integer, the number of numbers with the specified
// factor count, the number of prime factors found within the range and finally
// the average of the prime numbers found.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
	int first; // First integer value within range of tested values
	int last; // Last integer value within range of tested values
	int numoffactors; // Number of specified factors being tested for
	int counter1; // This is the first loop control variable
	int counter2; // This is the second loop control variable
	int counter3; // This is the variable to store the number of factors
	// that the given number has
	int counter4 = 0; // This is the variable to store the number of integer
	// values that have the specified factors
	int prime = 0; // This the variable to store number of prime numbers
	int primavg; // This is the average of all prime factors
	int primesum; // This is the sum of all prime factors

	cout << "\n";
	cout << "Jesse Yapi - Assignment #6 - Section #1003" << endl;
	cout << "\n";

	cin >> first >> last >> numoffactors;

	cout << "Numbers with at least " << numoffactors << " between " << first;
	cout << " and " << last << ":" << endl;
	for (counter1 = first, counter1 <= last, counter1 = counter1+1)
	{
		counter3 = 0;
		for (counter2 = 1, counter2 < counter1, counter2++)
		{
			switch (counter1 % counter2)
			{
			case 0:
				counter3++;
				if (counter3 >= numoffactors)
					counter4++;
				cout << counter1 << endl;
				break;
			}
		}
		// SYNTAX ERROR: You need braces in here somewhere
		if (counter3 == 2)
		prime++;
		primesum = primesum + counter1;
		else
		cout << endl;
	}
	cout << counter4 << " numbers with at least " << numoffactors << " factors found" << endl;
	cout << "Prime factors found: " << prime << endl;
	cout << fixed << showpoint << setprecision(4);
	cout << "Average of primes: " << primesum/prime << endl;

	return 0;
}
Last edited on
Hello thanks so much for your help. I put braces in here:

if (counter3 == 2)
{
prime++;
primesum = primesum + counter1;
else
cout << endl;
}

Unfortunately, this did not solve my problem. I am still receiving the exact same errors. Mainly:

error: expected â;â before â)â token
error: expected primary-expression at end of input
error: expected â;â at end of input
error: expected primary-expression at end of input
error: expected â)â at end of input
error: expected statement at end of input
error: expected â}â at end of input

That's not quite right. What does the else apply to?
Oooh, you're right. I see it now. I'm going to fix that and get back to you. Thanks.
I made a few adjustments (in italics) including fixing the braces and i'm still receiving the error code. i'm lost at this point. I just want to know what i'm doing wrong.

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
// Input: Data from a batch file containing 3 integer values. The first and                                                                                                                                        
// second values represent the range of values to be tested. The first value                                                                                                                                       
// will be less than or equal to the second value. The third integer value                                                                                                                                         
// represents the number of factors being tested for within the range of                                                                                                                                           
// values INCLUDING the first two integer values within the batch file.                                                                                                                                            
// Output: A list of numbers in the range that have the number of factors                                                                                                                                          
// specified by the third integer, the number of numbers with the specified                                                                                                                                        
// factor count, the number of prime factors found within the range and finally                                                                                                                                    
// the average of the prime numbers found.                                                                                                                                                                         

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
  
  int first;          // First integer value within range of tested values                                                                                                                                         
  int last;           // Last integer value within range of tested values                                                                                                                                          
  int numoffactors;   // Number of specified factors being tested for                                                                                                                                              
  int counter1;       // This is the first loop control variable                                                                                                                                                   
  int counter2;       // This is the second loop control variable                                                                                                                                                  
  int counter3;       // This is the variable to store the number of factors                                                                                                                                       
                       // that the given number has                                                                                                                                                                 
  int counter4 = 0;   // This is the variable to store the number of integer                                                                                                                                       
                      // values that have the specified factors                                                                                                                                                    
  int prime = 0;      // This the variable to store number of prime numbers                                                                                                                                        
  int primesum;       // This is the sum of all prime factors                                                                                                                                                      

  cout << "\n";
  cout << "Jesse Yapi - Assignment #6 - Section #1003" << endl;
  cout << "\n";

  cin >> first >> last >> numoffactors;

  cout << "Numbers with at least " << numoffactors << " between " << first;
  cout << " and " << last << ": ";
  for (counter1 = first, counter1 <= last, counter1++)
    {
      counter3 = 0;
      for (counter2 = 1, counter2 < counter1, counter2++)
        {
          switch (counter1 % counter2)
            {
            case 0:
            counter3++;
            if (counter3 >= numoffactors)
            counter4++;
            cout << counter1 << endl;
            break;
            }
        }
          if (counter3 == 2)
           {
              prime++;
              primesum = primesum + counter1;
            }
          else
            cout << endl;
    }
  cout << endl;
  cout << counter4 << " numbers with at least " << numoffactors
       << " factors found" << endl;
  cout << "Prime factors found: " << prime << endl;
  cout << fixed << showpoint << setprecision(4);
  cout << "Average of primes: " << primesum/prime << endl;

  return 0;
}


It keeps saying there is an error in line 41:
error: expected ‘;’ before ‘)’ token


then the rest of the errors are in line 72 which is the very last line of the code with the final brace. The errors in line 72 are:

error: expected primary-expression at end of input
error: expected â;â at end of input
error: expected primary-expression at end of input
error: expected â)â at end of input
error: expected statement at end of input
error: expected â}â at end of input

Any help I can get on this aspect is greatly appreciated. Thanks.
Last edited on
Last edited on
First, in your for loops use semicolon(;) not comma (,) to separate the initialisation statement, loop condition statement, and the update statement.
This will elimate errors at the compilation stage.

Secondly, initialize primesum to zero at line 31 or anyway before using it at line 59. This will elimate error at the execution stage.

Here is one way to go about it:

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
// Input: Data from a batch file containing 3 integer values. The first and                                                                                                                                        
// second values represent the range of values to be tested. The first value                                                                                                                                       
// will be less than or equal to the second value. The third integer value                                                                                                                                         
// represents the number of factors being tested for within the range of                                                                                                                                           
// values INCLUDING the first two integer values within the batch file.                                                                                                                                            
// Output: A list of numbers in the range that have the number of factors                                                                                                                                          
// specified by the third integer, the number of numbers with the specified                                                                                                                                        
// factor count, the number of prime factors found within the range and finally                                                                                                                                    
// the average of the prime numbers found.                                                                                                                                                                         

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
  
  int first;          // First integer value within range of tested values                                                                                                                                         
  int last;           // Last integer value within range of tested values                                                                                                                                          
  int numoffactors;   // Number of specified factors being tested for                                                                                                                                              
  int counter1;       // This is the first loop control variable                                                                                                                                                   
  int counter2;       // This is the second loop control variable                                                                                                                                                  
  int counter3;       // This is the variable to store the number of factors                                                                                                                                       
                       // that the given number has                                                                                                                                                                 
  int counter4 = 0;   // This is the variable to store the number of integer                                                                                                                                       
                      // values that have the specified factors                                                                                                                                                    
  int prime = 0;      // This the variable to store number of prime numbers                                                                                                                                        
  int primesum;       // This is the sum of all prime factors                                                                                                                                                      

  cout << "\n";
  cout << "Jesse Yapi - Assignment #6 - Section #1003" << endl;
  cout << "\n";

  cin >> first >> last >> numoffactors;

  cout << "Numbers with at least " << numoffactors << " between " << first;
  cout << " and " << last << ": ";
  //not(counter1 = first, counter1 <= last, counter1++) but
  for (counter1 = first; counter1 <= last; counter1++)
    {
      counter3 = 0;
      //not(counter2 = 1, counter2 < counter, counter2++) but
      for (counter2 = 1; counter2 < counter1; counter2++)
        {
          switch (counter1 % counter2)
            {
            case 0:
            counter3++;
            if (counter3 >= numoffactors)
            counter4++;
            cout << counter1 << endl;
            break;
            }
        }
          if (counter3 == 2)
           {
              prime++;
              /*You must initialize primesum before using it here, unless expect
              problem at execution */
              primesum = primesum + counter1;
            }
          else
            cout << endl;
    }
  cout << endl;
  cout << counter4 << " numbers with at least " << numoffactors
       << " factors found" << endl;
  cout << "Prime factors found: " << prime << endl;
  cout << fixed << showpoint << setprecision(4);
  cout << "Average of primes: " << primesum/prime << endl;

  return 0;
}
Thanks I will give this a try
Thanks so much for you help. It is compiling but my output is not what i'm looking for. I will take a closer look at my code to see if I can solve the problem. Those semicolons within the for loop statement was exactly what I needed and setting "primesum" to zero was spot on. Thank you!
Topic archived. No new replies allowed.