C++ - Strange problem with loop initialization - c++

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;
}

Related

expected initializer before ‘:’ token

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.

ERROR: slist was not declared in this scope

This code isn't working for me, I am getting below-mentioned compilation errors.
Somebody please help me to point out my mistake.
In function 'int main()':
4:3: error: 'slist' was not declared in this scope
4:9: error: expected primary-expression before 'int'
5:3: error: 'L' was not declared in this scope
12:9: error: expected primary-expression before 'int'
13:3: error: 'back' was not declared in this scope
Code:
int main() {
slist<int> L;
L.push_front(0);
L.push_front(1);
L.insert_after(L.begin(), 2);
copy(L.begin(), L.end(), // The output is 1 2 0
ostream_iterator<int>(cout, " "));
cout << endl;
slist<int>::iterator back = L.previous(L.end());
back = L.insert_after(back, 3);
back = L.insert_after(back, 4);
back = L.insert_after(back, 5);
copy(L.begin(), L.end(), // The output is 1 2 0 3 4 5
ostream_iterator<int>(cout, " "));
cout << endl;
}
All the problems boil down to the fact that the following declaration
slist<int> L;
throwed the error that
slist was not declared in this scope
You need to make sure slist is declared in your scope. include the necessary headers.

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

Capturing camera list on mac os x does not compile

So I've got a homework at school to list cameras available on mac OS X but I have to do it in C++ under xcode. I created such code:
#include <iostream>
#include <sstream>
#include <string.h>
#include <Quicktime/quicktime.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
int i = 0;
int selectedIndex;
cout << endl << "Let us select video device." << endl << "Available capture devices are:" << endl;
// first get a video channel from the sequence grabber
ComponentDescription theDesc;
Component sgCompID;
ComponentResult result;
theDesc.componentType = SeqGrabComponentType;
theDesc.componentSubType = 0L;
theDesc.componentManufacturer = 'appl';
theDesc.componentFlags = 0L;
theDesc.componentFlagsMask = 0L;
sgCompID = FindNextComponent (NULL, &theDesc);
seqGrabber = OpenComponent (sgCompID);
result = SGInitialize (seqGrabber);
result = SGNewChannel (seqGrabber, VideoMediaType, &videoChannel);
SGDeviceList theDevices;
SGGetChannelDeviceList(videoChannel, sgDeviceListDontCheckAvailability | sgDeviceListIncludeInputs, &theDevices);
if (theDevices)
{
int theDeviceIndex;
for (theDeviceIndex = 0; theDeviceIndex != (*theDevices)->count; ++theDeviceIndex)
{
SGDeviceName theDeviceEntry = (*theDevices)->entry[theDeviceIndex];
cout << i << ".1. " << theDeviceEntry.name << endl;
// name of device is a pstring in theDeviceEntry.name
SGDeviceInputList theInputs = theDeviceEntry.inputs;
if (theInputs != NULL)
{
int theInputIndex;
for ( theInputIndex = 0; theInputIndex != (*theInputs)->count; ++theInputIndex)
{
SGDeviceInputName theInput = (*theInputs)->entry[theInputIndex];
cout << i << ".2. " << theInput.name << endl;
// name of input is a pstring in theInput.name
}
}
}
} // i++ we need to add...
selectedIndex = 999;
if (i <= 0)
{
cout << "No devices found." << endl;
return 999;
}
else if (i == 1)
{
cout << "Default device will be used.\n" << endl;
selectedIndex = 0;
}
else
{
while (selectedIndex > i - 1 || selectedIndex < 0)
{
try
{
cin >> selectedIndex;
//string s;
//getline(cin, s, '\n');
//selectedIndex = boost::lexical_cast<int>(s);
}
catch(std::exception& e)
{
cout << "Please input index from 0 to " << i - 1 << endl;
selectedIndex = 999;
}
}
}
return selectedIndex;
}
It does not compile. It show lots of strange errors about SeqGrabComponentType but I am mac C++ nube and do not know what to do - how to make my app compile please help?
Update:
Errors list:
camerasList: In function 'int main()':
camerasList:49: error: 'SeqGrabComponentType' was not declared in this scope
camerasList:55: error: 'seqGrabber' was not declared in this scope
camerasList:56: error: 'SGInitialize' was not declared in this scope
camerasList:57: error: 'videoChannel' was not declared in this scope
camerasList:57: error: 'SGNewChannel' was not declared in this scope
camerasList:58: error: 'SGDeviceList' was not declared in this scope
camerasList:58: error: expected `;' before 'theDevices'
camerasList:59: error: 'sgDeviceListDontCheckAvailability' was not declared in this scope
camerasList:59: error: 'sgDeviceListIncludeInputs' was not declared in this scope
camerasList:59: error: 'theDevices' was not declared in this scope
camerasList:59: error: 'SGGetChannelDeviceList' was not declared in this scope
camerasList:66: error: 'SGDeviceName' was not declared in this scope
camerasList:66: error: expected `;' before 'theDeviceEntry'
camerasList:67: error: 'theDeviceEntry' was not declared in this scope
camerasList:70: error: 'SGDeviceInputList' was not declared in this scope
camerasList:70: error: expected `;' before 'theInputs'
camerasList:71: error: 'theInputs' was not declared in this scope
camerasList:76: error: 'SGDeviceInputName' was not declared in this scope
camerasList:76: error: expected `;' before 'theInput'
camerasList:77: error: 'theInput' was not declared in this scope
Update:
Half of problem solution: Compiling under i386 architecture solves most errors (few left).
Try to add #import <QuickTime/QuickTimeComponents.h> and link with QuickTime.framework.
As you found, compiling for i386 as opposed to x64 helps a lot.
For the rest, I added
ComponentInstance seqGrabber;
SGChannel videoChannel;
to your code, and it compiled successfully.