Need help in coding.

Ccoder

Junior Member
May 30, 2013
1
0
0
I'm a beginner in coding in C++. I just came to ask on how to loop your program.
Like for e.g, in my practice program.
Code:
using namespace std;

int main()
{
    cout << "One = Yes Two = No \n";
   float num1;
   float num2;
  int number;
  
   num1 = 1;
   num2 = 2;
   
   
   
   cout << "\n Yes or No?\n"<< endl;
   cin >> number;
   
 
   if (number == 1)
   {
            cout << "You have chosen 1.\n "<< endl;
            cout << " Restart program? \n Yes or No?";
            cin >> number;
            if(number==1)
            {
              //           
                         
            }
            else
            {
                // Just testing this.
                system("PLAY")
            }
            
   }
   else
   {
       if(number == 2)
       {
                 cout << "You have chosen 2. \n"<< endl;
       }
       else
       {
           cout << "You didn't follow the proper instructions.\n"<< endl;
           
       }
   }
   
 

   

    
    
    
    
    system("PAUSE");
    return 0;
}
I want the program to loop, right here.
Code:
cout << "You have chosen 1.\n "<< endl;
            cout << " Restart program? \n Yes or No?";
            cin >> number;
            if(number==1)
            {
              //           
                         
            }
            else
            {
                // Just testing this.
                system("PLAY")
            }
and right here, i want to be able to press 2 and have the program close.
Code:
else
            {
                // Just testing this.
                system("PLAY")
            }
 

piasabird

Lifer
Feb 6, 2002
17,168
60
91
This is the first thing you learn in programming. If you have a book I am sure there is an example for programming loops in the book!!!! i.e. get book Learning C++ in 24 hours or similar. Looks like homework.
 

you2

Diamond Member
Apr 2, 2002
5,998
1,145
126
do {
cout <<"Why am I doing this?\n";
} while (true);
while(true) {
cout <<"Why do i not see this line?";
}
exit(0);
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
use a label and goto
I don't want to be overly critical, but that's more of a C thing, isn't it?

Since Ccoder is coding C++, he should do it properly. With the new C++11 features and the C++ standard library you can do really beautiful loops (this requires a pretty up-to-date compiler, though):
Code:
#include <iostream>
#include <list>

void main() {
// we want to loop at least once, thus '1'
list<char> loop{'1'};
// if we want to keep looping more, just use this
// to extend the loop by '1' again!
auto again = [&]{loop.push_back('1');};
// keep looping 'auto' matically until loop is over
for (auto c : loop) {
  cout << "1 to loop, something else to exit" << endl;
  cin >> c; if (c == '1') again();
}
}
C++ :wub:
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
I don't want to be overly critical, but that's more of a C thing, isn't it?

Since Ccoder is coding C++, he should do it properly. With the new C++11 features and the C++ standard library you can do really beautiful loops (this requires a pretty up-to-date compiler, though):
Code:
#include <iostream>
#include <list>

void main() {
// we want to loop at least once, thus '1'
list<char> loop{'1'};
// if we want to keep looping more, just use this
// to extend the loop by '1' again!
auto again = [&]{loop.push_back('1');};
// keep looping 'auto' matically until loop is over
for (auto c : loop) {
  cout << "1 to loop, something else to exit" << endl;
  cin >> c; if (c == '1') again();
}
}
C++ :wub:

I'm gonna get frustrated for Fayd here. You realize he was being sarcastic, right? It's not apparent in your post if so.
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
I'm gonna get frustrated for Fayd here. You realize he was being sarcastic, right? It's not apparent in your post if so.
Please read my code. I'm sure its beauty will dispel all your uncertainty.
 

Elixer

Lifer
May 7, 2002
10,371
762
126
Hmmm...nothing like inline assembly for C/C++!

asm{
mov ax, this->vars
loopy:
...*
dec ax
cmp ax, 0
je exit
jmp loopy
exit:
}

* you need to add your own code to do what you want...you only asked for loop control...

On a more serious note, what is the reason people post these types of call for help when the answer is easily found using whatever search engine, or any good tutorial on said language ?
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
On a more serious note, what is the reason people post these types of call for help when the answer is easily found using whatever search engine, or any good tutorial on said language ?

It can be kinda hard to look for things on Google when you have zero knowledge about the topic. You don't really know what queries to use to find what you want and you have a hard time evaluating whether a certain result is crap or not.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
On a more serious note, what is the reason people post these types of call for help when the answer is easily found using whatever search engine, or any good tutorial on said language ?

I've often had the same thought. Perhaps people simply prefer to interact with other people, as opposed to a search engine. Then again, even simple questions tend to spawn discussion here, and other ideas sometimes come from that.
 

cytg111

Lifer
Mar 17, 2008
23,993
13,519
136
Hmmm...nothing like inline assembly for C/C++!

asm{
mov ax, this->vars
loopy:
...*
dec ax
cmp ax, 0
je exit
jmp loopy
exit:
}

* you need to add your own code to do what you want...you only asked for loop control...

On a more serious note, what is the reason people post these types of call for help when the answer is easily found using whatever search engine, or any good tutorial on said language ?

I would totally do that just to get on my professors/teachers nerve .. i can easily imagine the one eye-twitch on his face reading something like that.. heeeeh

on a serious note, industry standard mandates that you form a while loop like this ;
while(!!(!true!=!false))
{
.. your stuff
}
 
Last edited:

Chaotic42

Lifer
Jun 15, 2001
33,932
1,113
126
Hmmm...nothing like inline assembly for C/C++!

asm{
mov ax, this->vars
loopy:
...*
dec ax
cmp ax, 0
je exit
jmp loopy
exit:
}

* you need to add your own code to do what you want...you only asked for loop control...

On a more serious note, what is the reason people post these types of call for help when the answer is easily found using whatever search engine, or any good tutorial on said language ?

Why not just use ecx and loop?
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |