3 Replies - 305 Views - Last Post: 02 May 2019 - 01:52 PM Rate Topic: -----

#1 FamcodeX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 19

What is primary expression error?

Posted 02 May 2019 - 11:15 AM

My code was kinda like this, it is a very simple one as i am still learning. It is used to calculate bmi of people (body mass index) of formula bmi = weight / height^2.

#include <iostream>

int main() {
  double height, weight, bmi;
  int score = 0;
  int double height = 0;
  int bmi = 0;
  // Ask user for their height
  std::cout << "Type in your height (m): ";

  std::cin >>  height;
  // Now ask the user for their weight and calculate BMI
  double height = height * height
  std::cin >> weight;
   std::cout << "Type in your weight (kg): \n";
    bmi = weight / double height;
    std::cout << "bmi: \n"; 
  return 0;


It would be much appreciated if you try to help me how to improve this code too because i am still learning and started a few days ago. It is also my first time that I have asked something in a community forum. Thanks for all the help!

Is This A Good Question/Topic? 0
  • +

Replies To: What is primary expression error?

#2 modi123_1   User is online

  • Suitor #2
  • member icon

Reputation: 15081
  • View blog
  • Posts: 60,240
  • Joined: 12-June 08

Re: What is primary expression error?

Posted 02 May 2019 - 11:32 AM

Sorry, but I am not understanding your question.

If there is an error, please copy/paste it here.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2740
  • View blog
  • Posts: 4,388
  • Joined: 21-June 11

Re: What is primary expression error?

Posted 02 May 2019 - 11:43 AM

A primary expression is an identifier (i.e. a variable name), a numeric constant, a string literal or an arbitrary expression enclosed in parentheses.

There is no such thing as a primary expression error, but on many compilers syntax errors include the type of construct that would have been allowed at the given point in the program, so you might see a syntax error like "expected primary expression before ...".

If you're seeing that, it means that you likely wrote something that isn't part of an expression somewhere where an expression should go. As modi said, you should post the full error message if you want us to tell you more than that.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6256
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: What is primary expression error?

Posted 02 May 2019 - 01:52 PM

You keep creating duplicate and conflicting variables.

You declare a variable this way: <type> <variable name>. Like double height;.
Once you've declared a variable, you no longer use the type when you go to use the variable, otherwise the compiler thinks you're redeclaring it and will throw an error. Like height = 1.6;.

In your code, do you want to allow users to enter non-integer values for height and weight? In that case declare the variables as doubles, otherwise use ints.

This post has been edited by JackOfAllTrades: 02 May 2019 - 01:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1