#### Archived

This topic is now archived and is closed to further replies.

# C++ Looping

This topic is 6085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a problem with my small program, with this code, #include <iostream> using namespace std; int a; void main() { cout <<"Which table would you like me to multiply? "; cin >> a; } void working(); { cout <<"\n1X" << a << "=" << a*1; cout <<"\n2X" << a << "=" << a*2; cout <<"\n3X" << a << "=" << a*3; cout <<"\n4X" << a << "=" << a*4; cout <<"\n5X" << a << "=" << a*5; cout <<"\n6X" << a << "=" << a*6; cout <<"\n7X" << a << "=" << a*7; cout <<"\n8X" << a << "=" << a*8; cout <<"\n9X" << a << "=" << a*9; cout <<"\n10X" << a << "=" << a*10; cout <<"\n11X" << a << "=" << a*11; cout <<"\n12X" << a << "=" << a*12; cout <<"\n"; return 0 } I am trying to place in a loop, so if the user enters more than 20 it will repeat the line "Which table would you like me to multiply? " until numbers less than 20 has been entered.I have tried using the If statement but keep on getting werid errors and Im not to sure if its my Visual C++ compiler messing up. I have also tried using While and Do but it get bad loops which result in the program crashing. So can someone help me please?

##### Share on other sites
I''m sure its not your compiler thats the problem. Have you tried:
  do{ cout <<"Which table would you like me to multiply? "; cin >> a;}while(a>=20);

##### Share on other sites
int main() <-- main should not be "void"
{
.
.
.
return 0;
}

void work() <--- you had a ";" after "work()"
{
.
.
.
.
} <--- return without the "0" or even better no return at all.

-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

##### Share on other sites
Thanks guys, I tried the example Michalson gave me and I get a load of errors. One is "missing function header (old-style formal list?)" what does this mean?

##### Share on other sites
quote:
Original post by Malachi
Thanks guys, I tried the example Michalson gave me and I get a load of errors. One is "missing function header (old-style formal list?)" what does this mean?

It means that you haven''t posted the exact code that is causing the errors.

--
The Dilbert Principle: People are idiots.

##### Share on other sites
quote:

Thanks guys, I tried the example Michalson gave me and I get a load of errors. One is "missing function header (old-style formal list?)" what does this mean?

VC++ always tells the line where the error happens on. This error usually happens when you put a semicolon after a function like this:

void fuction(); <------ This made the error.
{
...
}

Jeff D

I hope that helped

Edited by - Jeff D on February 18, 2002 9:35:31 AM

##### Share on other sites
#include <iostream>
using namespace std;

int a;

int main()
do
{
cout <<"Which table would you like me to multiply? "; cin >> a;
}
while(a>=20);

cout <<"\n1X" << a << "=" << a*1;
cout <<"\n2X" << a << "=" << a*2;
cout <<"\n3X" << a << "=" << a*3;
cout <<"\n4X" << a << "=" << a*4;
cout <<"\n5X" << a << "=" << a*5;
cout <<"\n6X" << a << "=" << a*6;
cout <<"\n7X" << a << "=" << a*7;
cout <<"\n8X" << a << "=" << a*8;
cout <<"\n9X" << a << "=" << a*9;
cout <<"\n10X" << a << "=" << a*10;
cout <<"\n11X" << a << "=" << a*11;
cout <<"\n12X" << a << "=" << a*12;
cout <<"\n";

}
Ok this is new code I re done, now Ive placed in the While and Do loop I get 59 errors involing the lines of Cout. Which I dont understand because when I take out the loop it compiles fine.I just let people know that Im still a beginner at C++.

##### Share on other sites
If the code above is exactly from your project (ie copy and paste), you''re missing an opening brace right after main.

int main(){  // main body}

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

##### Share on other sites
Besides the syntax error, that code will go into a loop if the cin failbit gets set. Try it for yourself: when prompted for a number, enter a non-number. You should check the failbit via the fail() member function, and take appropriate action when it is set.

--
The Dilbert Principle: People are idiots.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
• ### Forum Statistics

• Total Topics
632933
• Total Posts
3009300
• ### Who's Online (See full list)

There are no registered users currently online

×