Switch to full style
For C/C++ coders discussions and solutions
Post a reply

declaring variables problem

Thu Sep 22, 2011 2:10 am

I am very new to c++ and am currently taking classes to learn the language. I am running into some errors when compiling and am not quite sure why. If anyone has any advice on where I went wrong it would help a ton! I think it is an error with not declaring a variable type. But if I am not mistaking I thought all the variables I used were declared at the very beginning of this function...

Here is the part of what I am trying to compile that is giving errors:
Code:
double sr(double z);
{
double x = 1;
double right =.5*(x+(z/x));
double diff = abs(right - x);

while (diff > 0.001)
{
x = right;
right = .5*(x+(z/x));
diff = abs(right - x);
}
return x;
}


And the errors I am getting when I compile:


bgi++ -Wall -g functions.cxx -o functions.exe
functions.cxx:77: error: syntax error before `{' token
functions.cxx:79: error: `x' was not declared in this scope
functions.cxx:79: error: `z' was not declared in this scope
functions.cxx:79: error: `x' was not declared in this scope
functions.cxx:80: error: use of `right' is ambiguous
functions.cxx:79: error: first declared as `double right' here
C:/Program Files (x86)/Colorado/cs1300/include/c++/3.3.1/bits/ios_base.h:808: error:
also declared as `std::ios_base& std::right(std::ios_base&)' here
functions.cxx:80: error: `x' was not declared in this scope
functions.cxx:82: error: syntax error before `while'
functions.cxx:85: error: ISO C++ forbids declaration of `right' with no type
functions.cxx:85: error: conflicting types for `int right'
functions.cxx:79: error: previous declaration as `double right'
functions.cxx:85: error: `x' was not declared in this scope
functions.cxx:85: error: `z' was not declared in this scope
functions.cxx:85: error: `x' was not declared in this scope
functions.cxx:86: error: ISO C++ forbids declaration of `diff' with no type
functions.cxx:86: error: conflicting types for `int diff'
functions.cxx:80: error: previous declaration as `double diff'
functions.cxx:86: error: use of `right' is ambiguous
functions.cxx:85: error: first declared as `int right' here
C:/Program Files (x86)/Colorado/cs1300/include/c++/3.3.1/bits/ios_base.h:808: error:
also declared as `std::ios_base& std::right(std::ios_base&)' here
functions.cxx:86: error: `x' was not declared in this scope
functions.cxx:87: error: syntax error before `}' token



Re: HELP with C++

Mon Sep 26, 2011 1:44 pm

I think problem is in the first line of code itself... It should not contain semicolon

Post a reply
  Related Posts  to : declaring variables problem
 PHP session variables problem in Firefox     -  
 Local variables vs Instance variables     -  
 Collections VS Set In Declaring     -  
 Declaring a Simple Function     -  
 PHP Variables     -  
 Casting Variables in php     -  
 Scope Variables in php     -  
 Apache Variables in php     -  
 Variables Are Assigned by Value     -  
 PHP session variables     -