How can I go back to the start of a program?

Feb 1, 2016 at 5:39pm
Hello, I'm writing a program for my brother. I'm a beginner in these things. So, what I want from a program is that it'll write random word and then ask continue? press y/n... if n/N, the program will exit, if y/Y, the program will start over by writing random word again. I already did the random word chooser, but I can't go back to the start of a program. How can I do this? Thanks for any respone and sorry for my english, it's not my first language.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

int contunue;

srand(time(0));

const string wordList[4] = { "icecream",
"computer", "dictionary", "algorithm" };

string word = wordList[rand() % 4];

cout << word << endl;

return 0;
}
Feb 1, 2016 at 5:55pm
I would advice using a do-while loop. This loop is always executed at least once. You would end up with something close to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    bool cont;
    srand(time(0));
    
    do
    {
         //Other code
         ...
         //The letter the user entered
         char letter;
         //Ask for a letter and read one
         cout << "Do you want another word? [Y/N] ";
         cin >> letter;
         //Set our continue flag, this will be true if the letter entered is Y or y
         cont = letter == 'Y' || letter == 'y';
     }while(cont);
     return 0;
}


The do-while loop is executed at least once, and goes on as long as the variable cont is set to true. It will be set to true whenever the letter the user entered is either Y or y, and it will be set to false otherwise, breaking the loop and thus stopping the program.
Last edited on Feb 1, 2016 at 5:57pm
Feb 1, 2016 at 6:20pm
Thanks you very much! It is working, but I've got another problem. The words that I put in can repeat for example first word is icecream, then i press y, then enter and it can be icecream again, can I do it like if (word = icecream) {//next command will delete icecream from the other words

Im really beginner at programing, and very young to programme more difficult programms tho.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char* argv[]) {


	bool cont;
    srand(time(0));
    
    do
    {
	

    const string wordList[4] = { "icecream",
        "computer", "dictionary", "algorithm" };

    string word = wordList[rand() % 4];

    cout << word << endl;
    
    char letter;
    
    cout << "Do you want another word? [Y/N] ";
         cin >> letter;
         //Set our continue flag, this will be true if the letter entered is Y or y
         cont = letter == 'Y' || letter == 'y';
         system("cls");
     }while(cont);
    
    
	
   
    return 0;

}
Last edited on Feb 1, 2016 at 6:25pm
Feb 1, 2016 at 6:39pm
You could use a vector to store your constants, and remove the elements as required. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector<string> values = {"icecream", "computer", "dictionary", "algorithm"}; //Initialize the vector
//Some other code
...
//The do-while loop
do
{
     //Fetch a new word
     string word = values[rand() % values.size()];
     //Print it
     cout << word << endl;
     //Removing the element
     vector<string>::iterator position = std::find(values.begin(),values.end(),word); //Find the position of our word
    values.erase(position); //And delete the element
    //Ask if we should go on
    ...
}while(cont && values.size() != 0); //Go on as long as there are still values left 
Last edited on Feb 1, 2016 at 6:43pm
Feb 1, 2016 at 6:55pm
So, I did it like this, but it's not working.
errors: http://prntscr.com/9xqnng
I don't know what to do now....




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char* argv[]) {
	
vector<string> values = {"icecream", "computer", "dictionary", "algorithm"}; //Initialize the vector

	bool cont;
    srand(time(0));
    
    do
    {
	

    const string wordList[4] = { "icecream",
        "computer", "dictionary", "algorithm" };

    string word = wordList[rand() % 4];

    cout << word << endl;
    
    char letter;
    
    cout << "Do you want another word? [Y/N] ";
         cin >> letter;
         //Set our continue flag, this will be true if the letter entered is Y or y
         cont = letter == 'Y' || letter == 'y' || 
         system("cls");
     }while(cont);
	 
	 do
	 {
	 	string word = values[rand() % values.size()];
	 	cout << word << endl;
	 	
	 	vector::iterator position = std::find(values.begin(),values.end(),word);
	 	
	 	values.erase(position);
	 	
	 	}
	 
	cout << "Bye bye!" << endl;    
    
	return 0;

}
Feb 1, 2016 at 7:01pm
Line 32 and 31 confused me?
Feb 1, 2016 at 7:24pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <ctime>
#include <vector>

int main() {

    std::srand( std::time(nullptr) ) ;
    std::vector<std::string> values = { "icecream", "computer", "dictionary", "algorithm" };

    bool cont = true ;
    while(cont) {

        const std::size_t pos = std::rand() % values.size() ;
        std::cout << values[pos] << '\n' ;

        values.erase( values.begin() + pos ) ;

        if( values.empty() ) {

            cont = false ;
            std::cout << "there are no more words.\n" ;
        }

        else {

            std::cout << "Do you want another word? [Y/N] ";
            char letter ;
            std::cin >> letter;
            cont = letter == 'Y' || letter == 'y' ;
        }
    }
}
Feb 1, 2016 at 7:26pm
Thanks you very much everyone! I couldn't do that without you. :)
Topic archived. No new replies allowed.