[C++] Loops

Junior Spellweaver
Joined
Jun 10, 2008
Messages
117
Reaction score
0
I am trying to put together a VERY VERY VERY VERY basic script and i want the program to go back to the beginning if it doesn't do what i want.


ie(THIS IS NOT PART OF MY SCRIPT)

somethinghere
cout<< "Please enter number #1" << "\n";
cin>> a;
if (a < 1){
cout<< "Please use a whole number" << endl;
goto somethinghere
}

can someone help me with this. please feel free to use the example to help
 
you're looking for a loop

For example
Code:
do
{
  cout<< "Please enter number #1" << "\n";
  cin>> a;
  if (a < 1)
    cout<< "Please use a whole number" << endl;
} while (a<1);
 
you're looking for a loop

For example
Code:
do
{
  cout<< "Please enter number #1" << "\n";
  cin>> a;
  if (a < 1)
    cout<< "Please use a whole number" << endl;
    
} while (a<1);
Well you could use a for, while, or do..while loop. The do..while loop is set-up so that it does the work before looking at the while statement. When it's done, it checks to see if it needs to run the script again. So it makes perfect sense in the case where you want to ensure the user sees it once, and hold them back until they input what you need. I don't believe that's what you're looking for. A regular while loop should be sufficient.

You should define your variable types.. And it might work better organized like this:
PHP:
int a;
cout<< "Please enter number #1" << "\n";
cin>> a;
while(a<1)
{
    cout<< "Please use a whole number" << endl;
    cin>> a;
}
cout<< "You did it!" << endl;

I'm sort of new to C++, hope that helps though :thumbup1:

Edit: For the include part, I found a source at microsoft for ya: http://msdn.microsoft.com/en-us/library/36k2cdd4(VS.80).aspx
 
how does that answer my new question?
Sorry, here:
Code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Source: ~ http://www.cplusplus.com/doc/tutorial/files/ ~
Hope that helps
 
how does that answer my new question?
Sorry, here:
Code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {

int a;
cout<< "Please enter number #1" << "\n";
cin>> a;
while(a<1)
{
    cout<< "Please use a whole number" << endl;
    cin>> a;
}
string files[2];
files[0] = "example.txt";
files[1] = "example2.txt";

int b = 0;

do
{
  ofstream myfile;
  myfile.open (files[b]);
  myfile << "Writing this to a file.\n";
  myfile.close();
  b++;
} while(files[b])
return 0;

}
Source: ~ http://www.cplusplus.com/doc/tutorial/files/ ~
Hope that helps

[Edited]
 
Sorry, here:
Code:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Source: ~ http://www.cplusplus.com/doc/tutorial/files/ ~
Hope that helps

All i wish to do is open it not edit it. like open an executable file. I dont like really long code as i said before... i think
 
Last edited:
All i wish to do is open it not edit it. like open an executable file. I dont like really long code as i said before... i think
If you don't like long code, seriously, quit, especially if you feel 5 lines is long. It's not like people give long examples just to be mean, if they're long it usually means it can't be done in a shorter way...

As for your current problem, you might get better search results if you try "executing" a file, cause "opening" a file in coding terms really means opening it and working with its contents in your own ways. If you execute a file from within your own program, it pretty much means you're telling another program to *open* it.
 
If you don't like long code, seriously, quit, especially if you feel 5 lines is long. It's not like people give long examples just to be mean, if they're long it usually means it can't be done in a shorter way...

As for your current problem, you might get better search results if you try "executing" a file, cause "opening" a file in coding terms really means opening it and working with its contents in your own ways. If you execute a file from within your own program, it pretty much means you're telling another program to *open* it.

i can handle long code it just makes it difficult to keep track of everything thats why i like splitting up.

i tried asking it to open it but it won't work.
for example(Negata i use examples for examples i don't give out the code i work on 5 lines doesn't bother me)

Code:
warrior.open("Warrior Squire.exe", ios::app);
       return 0;
it said that on a website that this will open it and finish out the rest of the command running in that window. But it opens it and exits out.
 
Your example is lacking, which type is warrior? And which website?

Well anyhow, it just sounds like you're opening the file for writing, with ios::app (for append) meaning all the data will add to the file at the end.

From a little googling your two options seem to be system("path to .exe") or ShellExecute (google for params), of which the former will block your program until it has finished and the latter just executes the file and will proceed on with your own code. I have never used either, though.
 
ok i ended up figuring out how to execute a program. By the way it was
Code:
WinExec("C:\\folder\\folder\\fie.exe", WM_SHOWNORMAL);

But since this place was for loops i need help with loops again.

this is tha code i have for a file i need help with
Code:
if(username == "alexz003" || username == "nonoowns", password == "blablabla")
	{
//things here
}// I want to put something here to make it to where if username != "alexz003"....... then go back to the if command at top without it running all the other stuff first

Please help me with that.
 
Code:
 goto loop1;

Code:
#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

string pass;
string username;

int main()
{
  loop1:
  cout << "Enter username\n\n";
  getline(cin, username);
  cout << "Enter password\n\n";
  getline(cin, pass);

 if (username == "lala" && password == "haha")
 {
  cout << "Welcome, put more **** here.\n\n";
}

else
{
  cout << "Wrong, little **** head.\n\n";
  Sleep (2000);
  goto loop1;
}

return 0;
}

I just woke up, not sure if I missed anything.
 
Most sources will tell you goto is very, very bad design and style and you should use real loop structures instead, and I wholly support this.

* The goto statement is never necessary; it can always be elimitated by rearranging the code.
* Use of the goto statement violates the rules of structured programming.
http://c.comsci.us/syntax/statement/goto.html

* The primary problem with goto is that it allows a programmer to cause the point of execution to jump around the code arbitrarily. This creates what is not-so-affectionately known as spaghetti code.
http://www.learncpp.com/cpp-tutorial/54-goto-statements/

* It is good programming style to use the break, continue, and return statement in preference to goto whenever possible.
http://msdn.microsoft.com/en-us/library/2c002fdz(VS.80).aspx

* Dijkstra argued that unrestricted GOTO statements should be abolished from higher-level languages because they complicated the task of analyzing and verifying the correctness of programs.
http://en.wikipedia.org/wiki/Goto#Criticism_of_goto_usage
 
how does that answer my new question?

Have a little more respect. He's explaining you some of the very basics of C/C++. If the only thing you're interested in is getting it to work, then quit programming, you won't be up for it. In programming, getting things to work is often easy, but getting it to work well, maintainable and efficient is the true challenge.

Also, the questions you ask are the most basic questions, they are the best documented / tutorialized questions on the internet. Use the search engine properly.

@Tman151
Argh, a goto statement! I see that as a bug honestly. C/C++ has much better alternatives, that keep your code readable.
 
FrostElite- when i do create an else statement it makes it to where if it is wrong it just closes for me. I want it to go back to the beginning to try again.

Negeta- I know that goto statements are the worst for C++ or any coding(except batch) in that sense, but i do need something that wil make it to where i can go back so i can have the person try again.

Daevius- I know that they are basic but whenever i looked it up it never went back to the beginning to have the user try again it just closes on me.

any help regarding this is greaty appreciated
 
Negeta- I know that goto statements are the worst for C++ or any coding(except batch) in that sense, but i do need something that wil make it to where i can go back so i can have the person try again.
Usually (if not always) tasks where you need to go back and repeat some steps are in essence loops. When going backwards in code you can easily avoid goto by using a for/while loop instead. A forward goto on the other hand is most often the case of an if-block.
 
Back