Loop

hello, i have a program that i need to repeat a section of it so many times,

the amount of times it needs to repeat is what the user enters at the start.

example, the user is asked how many times he wants to repeat,

then the part in the program will repeat that many times.

could you write out the example of it please?
I'm sure there's a tutorial on this somewhere. But in general there are loops where the checking is done at the beginning or the end of the loop, and some have a counted loop. As you should guess, you need a counted loop.

In C++, a loop that counts to four and prints the number can be written as:
1
2
for (int i = 0; i < 4; ++i)
    std::cout << i << std::endl;


I leave it to you to obtain a user specified value and add it to the loop.
I think you would do something like the following below, but I'm not sure. I'm a beginner at C++ myself so I'll see what I can do.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main() {
cout << "Please enter a number and allow the program to keep track of how many times: ";
int counter;
for (int input = 0; input < counter; counter ++) {
cin >> input;
cout << "You entered " << input << counter << "times." << endl;
}

return 0;
}
There you aren't initializing counter and the loop design isn't really correct
I see that now. Thanks for pointing that out. Logic is the hardest part for me in programming, but I love coding. Problem-solving is just something I'm working on so I can become a better coder.

Mike
//Try this one, i hope it help...

#include <iostream.h>
#include <conio.h>

main()
{
clrscr();
int counter;
cout<<"Please enter a number and allow the program to keep track of how many times: ";
cin>>counter;
for(int input=1;input<counter;input++)
{
cout<<"Number: "<<input<<endl;
}
cout<<"Number: "<<input<<" is the last number sir!!!"<<endl;
getch();
return 0;
}
That is bad:
iostream.h is deprecated
conio.h is old and non-standard
main with no type is non-standard
That is bad:
iostream.h is deprecated
conio.h is old and non-standard
main with no type is non-standard


I think this is not bad as long as you run smoothly your program.
Due to my observation you post only comments and not trying to post your code to help people getting learned! If you want to help post your code if not dont reply your messing out only this forum and you are helpless.
Your code won't compile on most C++ compilers, and I can link you to thousand of code examples I gave in the forums.
Posting bad code won't help anyone. Good code should be posted only when needed.
My comments to others' code helps them, see Mike W reply.
Last edited on
Bazzy wrote:
That is bad:
iostream.h is deprecated
conio.h is old and non-standard
main with no type is non-standard
us3rn4m3 wrote:
I think this is not bad as long as you run smoothly your program.

These are advices, which you should keep, as they are coming from a much more experienced programmer than you. You may be ok now with these habits, but you will start having problems, when you start using a new compiler or start working with other people.
us3rn4m3 wrote:
Due to my observation you post only comments and not trying to post your code to help people getting learned!

I don't see what to explain in code here, if you don't understand these simple advices, then there is a serious problem with you.
us3rn4m3 wrote:
If you want to help post your code if not dont reply your messing out only this forum and you are helpless.

This a forum, Bazzy and everybody else helping here does it in his/her free time. Your code (as Bazzy pointed it out too) is full of bad habits which new programmers (OP for example) shouldn't learn. I don't think it's appropriate to say something like this to somebody who cared to even look at your (unformatted) code, you should have rather learn from it.
closed account (z05DSL3A)
us3rn4m3, out of interest, what compiler are you using? Borland Turbo C++?
Yes sir im using Borland Turbo C++
That's why i ask Bazzy that my code is not bad maybe Bazzy don't use this compiler he, stick only on 1 compiler.If he know this compiler why he didn't mention my code that is use for Borland Turbo C++.Well if you are programmer you must be flexible using any kind of compilers, this is good for you if you experience working in company using other kind of compilers! If you are experience programmers as you see in my code you can analyze the sequence of code from algorithm, flowchart, and the source code.you can edit it if you are using other compiler but first learn to analyze the code.
And for Romai he/she post it.
I don't see what to explain in code here, if you don't understand these simple advices, then there is a serious problem with you.

I think you are out of your mind.1 question, are you a programmer? Why you can't see any explanation in those code? Are you blinded? Maybe you don't know how to analyze because you know only to memorize code maybe right for you is you have a serious problem! Romai learn to respect people around you before you post, my desire is to help newbie programmer that is willing to learn.
Before attacking other forum members you should think about some things:

Yes sir im using Borland Turbo C++
That's why i ask Bazzy that my code is not bad maybe Bazzy don't use this compiler he, stick only on 1 compiler.If he know this compiler why he didn't mention my code that is use for Borland Turbo C++.
Turbo C++ is an old compiler, pre-standard. ( according to Wikipedia the latest version is from 1993 ).
Pre-standard code will not compile with any modern compiler

If you are experience programmers as you see in my code you can analyze the sequence of code from algorithm, flowchart, and the source code.you can edit it if you are using other compiler but first learn to analyze the code.
The code you posted was meant to explain something to a beginner programmed, not an experienced one. As R0mai said, new programmers must not be exposed to old non-standard code as it will confuse them.

I think you are out of your mind.1 question, are you a programmer? Why you can't see any explanation in those code? Are you blinded? Maybe you don't know how to analyze because you know only to memorize code maybe right for you is you have a serious problem!
Still, your code was for a beginner. When posting code for a beginner you need to explain what it does as he/she may not have enough experience to understand it completely

Romai learn to respect people around you before you post, my desire is to help newbie programmer that is willing to learn.
Replace "Romai" with "us3rn4m3"
Before attacking other forum members you should think about some things:

is that what you think inside of your mind? Im not attacking but gives correction.Learn to analyze the situation before you post reply.You are the one who prepared to attack forum members, if you are really want to help so now post your updated code not to say anything to fight forum members! Post your code and not comments or to fight forum members! If you fight back OMG wutever xD
inb4
............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
Last edited on
Topic archived. No new replies allowed.