Invalid operands Errors/Expected ')' before ';' token

Page 1 of 1

2 Replies - 2929 Views - Last Post: 01 October 2012 - 10:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293820&amp;s=dc4b26e1869a31a6ce0407d410700088&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 bjm62692

Reputation: 0
• Posts: 25
• Joined: 23-September 12

Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 06:48 AM

This is my code.
```/*
Program Name: Pi
Author: Brittany McCord
Class: Introduction to Programming With C & MATLAB
Section: 1
Program Intent: Express Pi using two infinite series equations
Input Data: The number of terms to approximate by
Output Data: two separate approximations of pi
*/

/*---------------
Include Section
----------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

/*--------------------------- main function --------------------------------
Purpose: quantify values of pi using 2 equations and a specified number of terms
Returns: Approximate values of pi
----------------------------------------------------------------------------*/
int
main( ) {
long double approx1 (int terms);
long double approx2 (int terms);
int terms;
long double pi1;
long double pi2;
terms = 1;
while (terms != 0) {
printf("Enter the Number of Terms to Approximate pi, or 0 to Terminate: \n");
scanf("%n", &terms);
if (terms < 0) {
printf("Wrong Input. Please Try Again. \n");
}
else if (terms == 1) {
approx1 (terms);
printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);
approx2 (terms);
printf("The Second Approximation of pi to 1 term is: %.8Lf \n" &pi2);
}
if (terms > 1) {
approx1 (terms);
printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);
approx2 (terms);
printf("The Second Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi2);
}
}
printf("*****PROGRAM TERMINATED***** \n");
return (EXIT_SUCCESS);
}

/*--------------------------- approx1 funtion --------------------------------
Purpose: Approximates value of pi using the simplistic equation
Returns: Approximate value of pi
----------------------------------------------------------------------------*/
long double approx1 (int terms) {
long double pi1;
long double num;
long double denom;
int i;
num = 4;
denom = 1;
for (i = 1; i <= terms; i++) {
if (i % 2 != 0 ) {
pi1 += num / denom;
}
else {
pi1 -= num / denom;
}
denom += 2.0;
}
return pi1;
}

/*--------------------------- approx2 funtion --------------------------------
Purpose: Approximates value of pi using the square root equation
Returns: Approximate value of pi
----------------------------------------------------------------------------*/
long double approx2 (int terms) {
long double pi2;
long double num;
long double denom;
int i;
num = 12;
denom = 1;
for (i = 1; i <= terms; i++) {
if (i % 2 != 0 ) {
pi2 += num / (denom * denom);
}
else {
pi2 -= num / (denom * denom);
}
denom += 1.0;
}
pi2 = sqrt(pi2);
return pi2;
}

```

These are the errors.
```pi.c: In function 'main':
pi.c:39:75: error: invalid operands to binary & (have 'char *' and 'long double')
pi.c:41:76: error: invalid operands to binary & (have 'char *' and 'long double')
pi.c:45:77: error: invalid operands to binary & (have 'char *' and 'int')
pi.c:45:83: error: expected ')' before ';' token
pi.c:47:78: error: invalid operands to binary & (have 'char *' and 'int')
pi.c:47:84: error: expected ')' before ';' token
pi.c: In function 'approx1':
pi.c:65:17: error: expected ')' before ';' token
pi.c: In function 'approx2':
pi.c:88:17: error: expected ')' before ';' token

```

I do not completely understand what they are refering to. I have been fiddling around with it trying to fix it, but nothing seems to be working. Please help me understand the problem. Thank you.

Is This A Good Question/Topic? 0

Replies To: Invalid operands Errors/Expected ')' before ';' token

#2 jimblumberg

Reputation: 4736
• Posts: 14,763
• Joined: 25-December 09

Re: Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 07:08 AM

It looks like you need to study up on the documentation of printf() in the following code:
```           printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);
```

You are passing the address of pi1 as a parameter, you only need to pass the variable, you don't need an ampersand. But you do need a comma between the first parameter and the second.
```           printf("The First Approximation of pi to 1 term is: %.8Lf \n", pi1);
```

In the following code you have the same problem as above plus you have substituted a semicolon for a comma.
```            printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);
```

It should be:
```            printf("The First Approximation of pi to %n terms is: %.8Lf \n", terms, pi1);
```

Jim

This post has been edited by jimblumberg: 01 October 2012 - 08:13 AM

#3 bjm62692

Reputation: 0
• Posts: 25
• Joined: 23-September 12

Re: Invalid operands Errors/Expected ')' before ';' token

Posted 01 October 2012 - 10:47 AM

jimblumberg, on 01 October 2012 - 07:08 AM, said:

It looks like you need to study up on the documentation of printf() in the following code:
```           printf("The First Approximation of pi to 1 term is: %.8Lf \n" &pi1);
```

You are passing the address of pi1 as a parameter, you only need to pass the variable, you don't need an ampersand. But you do need a comma between the first parameter and the second.
```           printf("The First Approximation of pi to 1 term is: %.8Lf \n", pi1);
```

In the following code you have the same problem as above plus you have substituted a semicolon for a comma.
```            printf("The First Approximation of pi to %n terms is: %.8Lf \n" &terms; &pi1);
```

It should be:
```            printf("The First Approximation of pi to %n terms is: %.8Lf \n", terms, pi1);
```

Jim

This is a huge help. Thank you so much. I am completely new to programming, so I tend to make the more obvious errors.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }