2

How do I display 3 names at a time, pausing to let the user press a key before the list continues displaying.

My code now only loops the first 3 values of the array

#include <iostream> 
#include <string> 
#include <iomanip> 

using std::setw; 
using namespace std; 

int main() { 

    string a; 
    string names[10]; //size of array 

    for (int i = 0; i < 10; i++) 
    { 
        std::cout << "Enter name "; 
        std::cin >> a; //user input 

        names[i] = a; //assigns input to array 
    } 
    cout << "\n"; 

    for (int k = 0; k < 10; k++) 
    { 
        for (int j = 0; j < 3; j++) 
        { 
            cout << names[j] << endl; 
        } 

        system("pause"); 
    } 

}
hahas018
  • 23
  • 6
  • You can use `sleep()`. I would probably use another variable and ++ after printing and when it hits three do and `if` and `sleep()` – I'm here for Winter Hats Jan 17 '17 at 16:18
  • 1
    `using std::setw`, *then* you gave up and just dumped everything from `std` in the global namespace? Point for trying, I guess... – Quentin Jan 17 '17 at 16:42

3 Answers3

3

I changed the answer based on your comment. Instead of sleeping we just pause and wait until user inputs anything into the keyboard. Also a note, since you're using namespace, you don't need to include std::, I decided to use it since I was unsure what way you wanted.

#include <iostream>
#include <string>
#include <iomanip>

using std::setw;
using namespace std;

int main() {

    string a;
    int pauseCheck = 0; //new var
    string names[10]; //size of array

    for (int i = 0; i < 10; i++) {
        std::cout << "Enter name ";
        std::cin >> a; //user input

        names[i] = a; //assigns input to array
    }
    cout << "\n";

    for (int k = 0; k < 10; k++) {

        cout << names[k] << endl;
        pauseCheck++; //increments check

        if (pauseCheck == 3) { //if equals 3
            system("pause"); //we pause till user input
            pauseCheck = 0; //reset pause check
        }
    }
    system("pause"); //last and final pause before program ends
}
  • Hi, thx for helping! However, is there another function other than sleepCheck? I'm supposed display the names 3 at a time, pausing to let the user press a key before the list continues displaying. – hahas018 Jan 17 '17 at 17:22
  • @hahas018 please see the revised answer. Simply instead of sleeping for x amount of time we can pause using the same code instead. – I'm here for Winter Hats Jan 17 '17 at 18:14
0

Here's another way of doing it which I think is a little more straight forward:

#include <iostream> 
#include <string> 
#include <iomanip> 

using std::setw;
using namespace std;

int main() {

    string a;
    string names[10]; //size of array 

    for (int i = 0; i < 10; i++)
    {
        std::cout << "Enter name ";
        std::cin >> a; //user input 

        names[i] = a; //assigns input to array 
    }
    cout << "\n";

    for (int k = 0; k < 10; k++)
    {
        cout << names[k] << endl;

        if((k + 1) % 3 == 0) // everytime k + 1 is divisible by 3, let user hit a key
            system("pause");
    }
}
bfair
  • 1,101
  • 7
  • 16
-1

You can wait for a Enter-Press with another std::cin, just write into an garbage value.

I think other ways are not platform independent. You could ofcourse use the windows api, or unix stuff to get a key press.

jonas_toth
  • 872
  • 5
  • 8
  • You can wait until user inputs anything into the keyboard using `system("pause");`, just as the user had at the end of his question. This is used rather than using a `cin` that does nothing. – I'm here for Winter Hats Jan 17 '17 at 18:29
  • no you cant. linux does not have such a command. its and windows only option. in that case, std::cin would work platform independent, thats what i meant with my answer – jonas_toth Jan 17 '17 at 20:48