Connecting Tech Pros Worldwide Forums | Help | Site Map

int main error

Newbie
 
Join Date: Oct 2009
Posts: 9
#1: Oct 7 '09
I am very new with C++
I have an assignment that follows

Write a C++ program containing a for loop to print the message
DooBeeDooBeeDooBeeDooBeeDooBeeDooBee Do

My code so far is

#include <iostream>
using namespace std;

int main ()
{
int Doo;
int Bee;

for (int Doo = 0; Doo < 6; Doo++)
for (int Bee = 0; Bee < 6; Bee++)
std::cout << "Bee"<<std::;
std::cout << "Doo"<<std::;
}




My error messages follows

DooBee.cpp: In function âint main()â:
DooBee.cpp:11: error: expected unqualified-id before â;â token
DooBee.cpp:12: error: expected unqualified-id before â;â token

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#2: Oct 7 '09

re: int main error


Expand|Select|Wrap|Line Numbers
  1. std::cout << "Bee"<<std::;
Did you mean:

Expand|Select|Wrap|Line Numbers
  1. std::cout << "Bee"<<std::endl;
?

Also, the int Bee in main() is a different variable from the int Bee that's inside the for loop.
Newbie
 
Join Date: Oct 2009
Posts: 9
#3: Oct 7 '09

re: int main error


I didn't mean endl I need it all on one line.

What do you suggest to fix the main ()
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,382
#4: Oct 7 '09

re: int main error


Take the word int out of your for loops.

What this is doing is creating another int named Bee that is local to the loop. This local variable is hiding the one you defined outside the loop. Do this for both variables.
Newbie
 
Join Date: Oct 2009
Posts: 9
#5: Oct 7 '09

re: int main error


Done still got these three messages


DooBee.cpp: In function âint main()â:
DooBee.cpp:11: error: expected unqualified-id before â;â token
DooBee.cpp:12: error: expected unqualified-id before â;â token
Newbie
 
Join Date: Oct 2009
Posts: 9
#6: Oct 7 '09

re: int main error


New Code is

#include <iostream>
using namespace std;

int main()
{
int Doo;
int Bee;

for (Doo = 0; Doo < 6; Doo++)
for (Bee = 0; Bee < 6; Bee++)
std::cout << "Bee"<<std::;
std::cout << "Doo"<<std::;
}
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,580
#7: Oct 7 '09

re: int main error


Shouldn't it be this? (It should compile but I know it won't do what he wants.)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int
  5. main()
  6. {
  7. int Doo;
  8. int Bee;
  9.  
  10. for (Doo = 0; Doo < 6; Doo++)
  11. for (Bee = 0; Bee < 6; Bee++)
  12. cout << "Bee";
  13. cout << "Doo";
  14. }
  15.  
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#8: Oct 7 '09

re: int main error


A loop without braces is only done for the next one line of code. If you want it done for two or more lines you have to enclose your loop in braces;
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0 ; i < 10 ; i++)
  2. std::cout << i;
  3. std::cout << i;
  4.  
is different from
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0 ; i < 10 ; i++){
  2. std::cout << i;
  3. std::cout << i;
  4. }
  5.  
Regards,

Alex.
Newbie
 
Join Date: Oct 2009
Posts: 9
#9: Oct 7 '09

re: int main error


I am sorry for the newbish question but after i compile it how do I run it
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#10: Oct 7 '09

re: int main error


That rather begs the question how are you compiling it?

From the command line? From a IDE? If its from an IDE which IDE (for example Visual C++, Eclipse, Dev-C++, CodeBlocks)?

On what operating system?
Newbie
 
Join Date: Oct 2009
Posts: 9
#11: Oct 7 '09

re: int main error


Command Line inside WinSCP
Windows 64bit vista
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#12: Oct 7 '09

re: int main error


Which compiler are you using?

Anyway regardless of that the result of a compilation, assuming no errors, should be an object file, extension .obj on a Windows machine. You would then link the object with libraries and other objects to produce a executable file, extension .exe.

However most modern compiler interfaces are actually interfaces onto the entire tool chain. This means you can initiate a compile followed by a link from a single command.

If you have a .exe in the directory you are compiling in then that is what has happened. If you only have a .obj then you have compiled but not linked. In the latter case try telling the compiler you would like an executable output, normally with the -o <outputfilename> switch. Give your output file name a .exe extension (this works with gcc/g++ I am not so sure about the Microsoft compiler).
Newbie
 
Join Date: Oct 2009
Posts: 9
#13: Oct 8 '09

re: int main error


Oh im sorry im not trying to run it independent. I just want to check it in the command line to make sure its working.
Familiar Sight
 
Join Date: Jan 2007
Posts: 193
#14: Oct 8 '09

re: int main error


Do you mean the last Do has to have a space in front of it?
If not something like this may serve..
Expand|Select|Wrap|Line Numbers
  1. string s1="DooBee";
  2. string s2="Do";
  3.     for(int i=0;i<6;++i)
  4.     if(i==5)cout<<s2;
  5.     else cout<<s1;
otherwise the second last line should read:
Expand|Select|Wrap|Line Numbers
  1. if(i==5)cout<<" "<<s2;
Newbie
 
Join Date: Oct 2009
Posts: 9
#15: Oct 8 '09

re: int main error


Excellent responses and suggest, I will be on these forums for a long time to come
Newbie
 
Join Date: Oct 2009
Posts: 9
#16: Oct 8 '09

re: int main error


Last Question for this topic I promise lol.

The program compiles and runs smooth but it prints out at the very beginning of my command line.

Is there a way to put in a endline to fix this
Reply

Tags
c++