**Essentially I was given pseudo code:
"x = 1
repeat 10 times: x = (x + n / x) / 2
return x"
And the pseudo code for the int main function (int main function to print out my n values in the cout) at the end, in order to create a sqrt function program. I get the following errors on linux2 compiler:
: In function ‘double my_sqrt_1(double)’:
:9:1: error: expected primary-expression before ‘return’
:9:1: error: expected ‘;’ before ‘return’
: In function ‘int main()’:
:
15:13: error: expected unqualified-id before ‘-’ token
:~> expected primary-expression before ‘return’
Help is much appreciated!
#include <iostream>
#include <math.h>
using namespace std;
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2 <<
return x;
}
int main()
{
int n= 3.141459;
int k= -100,-10,-1,0,1,10,and 100;
for(auto k : { -100,-10,-1,0,1,10,100}){
n=3.14159 * pow (10.0,k);
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
return 0;
}
}
You missed a semicolon at the end of the cout line:
double my_sqrt_1(double n)
{
for (int x= 1; x<10; ++x)
cout<< x << '\t' << x=(x+n/x)/2;
return x;
}
The clue is in the error:
:9:1: error: expected ‘;’ before ‘return’
Finding the source of compiler errors can be tricky for those new to C/C++, if you miss a semi-colon the line reported will often differ from the one containing the actual error. As in this case where the return line became part of the same statement as the line above.
Also here:
int k= -100,-10,-1,0,1,10,and 100;
That is not how you define an array, you should read up on the basics of those since you're new to the game, which is evident here:
cout << "print n,sqrt(n),and my_sqrt_1(n)" ;
Where you're not calling any functions but instead outputting a static string of text. You need to make the function calls and variable outputs outside of the literal string:
cout << "print " << n << "," << sqrt(n) << ", and" << my_sqrt_1(n);
Related
The compiler can't handle even the simplest loop
#include <iostream>
using namespace::std;
int main()
{
for( int i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says this:
prog.cpp: In function ‘int main()’:
prog.cpp:7:18: error: expected unqualified-id before ‘char’
prog.cpp:7:18: error: expected ‘;’ before ‘char’
prog.cpp:7:39: error: expected ‘)’ before ‘;’ token
prog.cpp:7:41: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
prog.cpp:7:41: note: (if you use ‘-fpermissive’ G++ will accept your code)
prog.cpp:7:46: error: ‘a’ was not declared in this scope
prog.cpp:7:50: error: expected ‘;’ before ‘)’ token
And yes, I know I can initialize 'i' and 'a' before the loop. So let's try:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
for(i = 0, char a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Compiler says:
prog.cpp: In function ‘int main()’:
prog.cpp:8:13: error: expected primary-expression before ‘char’
prog.cpp:8:13: error: expected ‘;’ before ‘char’
prog.cpp:8:41: error: ‘a’ was not declared in this scope
When option -std=c++11 used:
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: error: expected unqualified-id before ‘char’
prog.cpp:7:17: error: expected ‘;’ before ‘char’
prog.cpp:7:38: error: expected ‘)’ before ‘;’ token
prog.cpp:7:40: error: ‘i’ was not declared in this scope
prog.cpp:7:45: error: ‘a’ was not declared in this scope
prog.cpp:7:49: error: expected ‘;’ before ‘)’ token
Last one:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(i = 0, a = 'A'; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Works fine. Guys, am I blind or something? Maybe you need my arch and compiler version:
uname -a
Linux freelancer 3.2.0-4-686-pae #1 SMP Debian 3.2.63-2+deb7u2 i686 GNU/Linux
g++ --version
g++ (Debian 4.7.2-5) 4.7.2
You cannot declare items of different types in the same declaration.
This is true inside and outside of loops. You're not "blind", it's just not valid C++.
Here's the right code:
#include <iostream>
using namespace::std;
int main()
{
int i = 0;
char a = 'A';
for(; i <= 26; i++, a++ )
cout << "OK, lets try. Showing values: i = "
<< i << ", a = " << a << endl;
}
Your working version is also valid because the declaration can be swapped out for an expression, though in your case it's redundant because those variables already hold those values at the start of the loop.
26 is so tiny number, thus you can do
#include <iostream>
int main()
{
for( char i = 0, a = 'A'; i <= 26; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}
Or even IMHO more clear code, more clear aim, iterating from 'A' until 'Z'.
int main()
{
for( char i = 0, a = 'A'; a <= 'Z'; i++, a++ )
std::cout << "OK, lets try. Showing values: i = "
<< static_cast<int>(i) << ", a = " << a << std::endl;
}
My code looks likes below:
#include <vector>
#include <iostream>
#include <map>
using namespace std;
int main()
{
std::map<int, std::vector<int> > map;
map[1].push_back(5);
map[1].push_back(3);
map[3].push_back(2);
map[2].push_back(1);
map[1].push_back(-1);
map[3].push_back(2);
int sum2 = 0;
for (const pair<int, vector<int> >& index_vec : map)
{
int sum = 0;
for (int elem : index_vec.second)
{
sum += elem;
}
sum2 += sum*sum;
cout << "index " << index_vec.first << ": " << sum << endl;
}
cout << "sum_2: " << sum2 << endl;
return 0;
};
Which works fine in my laptop but when using desktop gives me following erors:
map.cpp: In function ‘int main()’:
map.cpp:17: error: expected initializer before ‘:’ token
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘;’ at end of input
map.cpp:29: error: expected primary-expression at end of input
map.cpp:29: error: expected ‘)’ at end of input
map.cpp:29: error: expected statement at end of input
map.cpp:29: error: expected ‘}’ at end of input
The output should be:
index 1: 7
index 2: 1
index 3: 4
sum_2: 66
which my laptop gives as expected. I have absolutely no idea, could soeone please help me out?
Your code works on me without any modification. Please check your compiler. You will need a C++11 compiler. If you use g++, something like this:
g++ -std=c++11 A.cpp
https://gcc.gnu.org/projects/cxx-status.html#cxx11. You need at least 4.7. Please update your ancient compiler.
I am programming in C++ and want to use the powerful built-in functions in Octave. I followed the standalone programguideline on Octave website. I can run the function norm (which is called as Fnorm in C++) with the sample code successfully. Now I want to use the function mldivide to solve a linear equation.
#include <iostream>
#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>
octave_value_list input;
octave_value_list retval;
int main (void) {
Matrix A(4,4);
for (octave_idx_type i = 0; i < 4; i++)
for (octave_idx_type j = 0; j < 4; j++)
A(i,j) = 1.0 / (static_cast<double> (i) +static_cast<double> ( j ) + 1.0 ) ;
ColumnVector b(4,1.0);
input.append(A);
input.append(b);
retval=Fmldivide(input);
ColumnVector x =retval(0).column_vector_value();
std::cout << "A = " << std::endl << A << std::endl
<< "b = " << std::endl << b << std::endl
<< "x = " << std::endl << x << std::endl;
return 0;
}
But there are errors as follow.
main.cpp: In function 'int main()':
main.cpp:23:26: error: invalid initialization of reference of type 'const octave_value_list&' from expression of type 'Matrix'
In file included from main.cpp:3:0:
/usr/local/octave/3.8.0/include/octave-3.8.0/octave/../octave/builtin-defun-decls.h:198:1: error: in passing argument 1 of 'octave_value_list Fmldivide(const octave_value_list&, int)'
Any thoughts?
There are two reasons why this is not working:
Fmldivide takes an octave_value_list as input
Fmldivide returns an octave_value_list but you are declaring a ColumnVector
You can fix the first by:
converting your input into a single octave_value and leaving the conversion to octave_value_list to the compiler:
Fmldivide (octave_value (b), 1);
use do_binary_op with the left division operator:
do_binary_op (octave_value::op_ldiv, b, 1);
You can fix the second by using the column_vector_value method:
if you are using Fmldivide you get a list, so you must index the first element first:
ColumnVector x = Fmldivide (b, 1)(0).column_vector_value ();
if you use do_binary_op you will get an octave_value so indexing is unecessary:
ColumnVector x = do_binary_op (octave_value::op_ldiv, b, 1).column_vector_value ();
Trying to add command line arguments to my programs. So I was experimenting and cannot figure out this intellisense warning for the life of me. It keeps on saying it is expecting a ')', but I have no idea why.
Here is the code it does not like:
// Calculate average
average = sum / ( argc – 1 );
Then it underlines the subtraction operator. Below is the full program.
#include <iostream>
int main( int argc, char *argv[] )
{
float average;
int sum = 0;
// Valid number of arguments?
if ( argc > 1 )
{
// Loop through arguments ignoring the first which is
// the name and path of this program
for ( int i = 1; i < argc; i++ )
{
// Convert cString to int
sum += atoi( argv[i] );
}
// Calculate average
average = sum / ( argc – 1 );
std::cout << "\nSum: " << sum << '\n'
<< "Average: " << average << std::endl;
}
else
{
// If invalid number of arguments, display error message
// and usage syntax
std::cout << "Error: No arguments\n"
<< "Syntax: command_line [space delimted numbers]"
<< std::endl;
}
return 0;
}
The character you think is a minus sign is something else, so it is not parsed as a subtraction operator.
Your version:
average = sum / ( argc – 1 );
Correct version (cut and paste into your code):
average = sum / ( argc - 1 );
Note that calculating an average using integers might not be the best way to do it. You have integer arithmetic on the RHS, which you then assign to float on the LHS. You should perform the division using floating point types. Example:
#include <iostream>
int main()
{
std::cout << float((3)/5) << "\n"; // int division to FP: prints 0!
std::cout << float(3)/5 << "\n"; // FP division: prints 0.6
}
I tried to compile your code on my machine with g++ 4.6.3 and got the follow error:
pedro#RovesTwo:~$ g++ teste.cpp -o teste
teste.cpp:20:8: erro: stray ‘\342’ in program
teste.cpp:20:8: erro: stray ‘\200’ in program
teste.cpp:20:8: erro: stray ‘\223’ in program
teste.cpp: Na função ‘int main(int, char**)’:
teste.cpp:16:33: erro: ‘atoi’ was not declared in this scope
teste.cpp:20:35: erro: expected ‘)’ before numeric constant
Looks like there is some strange char in that line. Remove and re-write the line fixed the error.
While trying to write this code, I get an error "cin doesnt name a type".
I don't know what the problem is exactly and I tried to write "using namespace std;"
but it gave the same error.
Here's the code
#include<iostream>
namespace myStuff {
int value = 0;
}
using namespace myStuff;
int main {
std::cout << "enter integer " << ;
std::cin >> value;
std::cout << "\nyouhaveenterd a value" << value ;
return 0;
}
Here's the compilation error :
: extended initializer lists only available with `-std=c++0x` or `-std=gnu++0x` [enabled by default]|
: expected primary-expression before ‘;’ token|
expected `}` before `;` token|
`cin` does not name a type|
: `cout` does not name a type|
: expected unqualified-id before `return`|
: expected declaration before `}` token|
||=== Build finished: 6 errors, 1 warnings ===|
int main{
should be
int main(){
and
std::cout << "enter integer " << ;
should be
std::cout << "enter integer ";
On this line:
std::cout << "enter integer " << ;
There's no corresponding operand to make the statement syntactically valid. That's probably the source of your errors.
Its the previous line.
cout<<"enter integer" **<<** ;
that last << is expecting an argument which is never given