Code Blocks 13.12 C++ basic for loop debug error - c++

I am coding in C++ in Code Blocks 13.12 with TDM-GCC compiler version 4.8.1 on Windows 10 64-bit, but while in any other PC I have used (both at school and university wtih Win7 64-bit and Win8 64-bit, respectively) there were no problems with debugging and running programs, in my case I am not even able to compile simple "for" loop. I tried all versions of CB, withou success.
Here is the siplest code, that gives errors:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0,i < 9,i++)
cout << "Test, ",i << endl;
return 0;
}
As you can see, it is a sightly modified basic console application in C++.
The errors I get are following:
error: expected initializer before '<' token
error: expected ';' before '<' token
error: expected primary-expression before '<' token
error: expected ';' before ')' token
error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
I tried everything - form installing different versions of code blocks, to disableing anti-virus software.
Can you tell me what have I been doing wrong?

Your for loop syntax is wrong.
It should be:
for (int i = 0; i < 9; ++i) and also add the {} after the ).
And also
cout << "Test, " <<i << endl; //and also the concatenation was wrong
Hope it helps.

The curly braces are not necessary if the loop body contains only one expression.
You should change commas to semicolons between the braces after "for". "for" is not a function, it is a keyword and the statement "int i=0" is not a parameter.
Here is the right code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0; i < 9; i++)
cout << "Test, " << i << endl;
return 0;
}

Related

Macro for push_back giving problems

I am starting to code in c++.
I was learning macros when this happenned:
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
int main() {
vector<int> hello;
hello.PB(3);
hello.PB(2);
cout << hello < "\n";
}
My compiler shows, pointing to line 3:
Error: statement cannot resolve address of overloaded function
For your code I get problems with the < instead of << and what I assume the main problem:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<int>’)
cout << hello << "\n";
It is telling you that there is no known way to output a whole vector to cout.
The simple way to fix that is
cout << hello[0] << " " << hello[1] << "\n";
This gets you an output of
3 2
The more complex way, with more convenient result, is to do the overloading yourself accordingly.

I'm trying to slice my c++ array from the second to the last index

I'm trying to emulate the echo command in C++.
I'm trying to slice the the program name off of the entry values and push the
rest to the command line. BUT I'm getting weird errors.
Here's my code:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
if(argv[1] == "echo"){
cout << args[2:];
}
return 0;
}
But I get the error(s):
cmd.cpp: In function 'int main(int, const char**)':
cmd.cpp:6:13: error: 'args' was not declared in this scope
cout << args[:];
^~~~
cmd.cpp:6:13: note: suggested alternative: 'argc'
cout << args[:];
^~~~
argc
cmd.cpp:6:18: error: expected primary-expression before ':' token
cout << args[:];
^
cmd.cpp:6:18: error: expected ']' before ':' token
cout << args[:];
^
]
I am trying to take {1234545, "hello", ", world!"} and turn It into "hello, world!" Basically what I want to do is get rid of array[0] and joining the rest of the list together.
EDIT: Thanks #chipster for giving a great answer!
Minor issue (I mean, I guess it's a big one, as it's the one causing the compiler error, but once you fix it, you're going to get rammed with another error really quick, so...): args does not exist. You actually want argv instead.
The syntax arr[i:j] it Python syntax, not C++.
To do the equivalent in C++, do this instead:
for(int i=2;i<argc;i++) {
std::cout << argv[i] << "\n"; // "\n" is just to make things look nicer.
// "\n" could be any separator
}

Outputting cerr using cout

I came across a piece of code that does basically the following:
#include <iostream>
using namespace std;
int main()
{
cout << cerr << " Hi.";
return 0;
}
Output:
0x601088 Hi.
First of all, why would anyone do 'cout << cerr' it does not make sense.
Second of all, what is the meaning of the output above?
Worth to mention that on my machine the above code compiles and executes without errors.
However a much more complex code (doing the same thing as above) on a different machine (server ssh connection) running the same version of gcc 5.4.0, produces this error when doing make (shortened for clarity):
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
cout << cerr << "DB: Field " + e.table + "[" + e.index + "]." + e.field
Any thoughts on this?
Until c++11, std::basic_ios offered an implicit conversion to void*. This code won't compile with c++11 or later. You basically have this, which compiles with older versions of gcc :
#include <iostream>
int main()
{
void * x = std::cerr;
std::cout << x << " Hi.";
return 0;
}

Errors in a sqrt function program in c++

**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);

Why am I getting the error "cin does not name a type"

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