Input Looping Statement

Hello community, I am trying to make a program that can make 1-10 inputs with 70 characters using loop statement but everytime I run my code it only prints one line like this :

3
a
b
c

Output:
a
a
a
a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
using namespace std;
main()

{
int a,s;
char word[70];	// The maximum of characters needed 

cin >> a;  //Asking the user how inputs need.

if(a<10) // To make sure that the program will stop if the user enters the exceeding number of lines
{

for(int i=0;i<= a;i++)
{
cin.getline(word,70);
} 	// The user will type anything but in limited characters

for(int i=0;i<=a;i++)
{
 cout << word;
}


Last edited on
for loop is in integers not char, also line one your missing a # in front of iostrem

its your for loop it replaces the previous char

1
2
3
4
for(int i=0;i<= a;i++)
{
cin.getline(word,70);
} 
Last edited on
Edited.

I'm sorry loop statement really gives me confusion, any alternative codes to make it work ?
Please, take a glance at the following code for hints:
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
51
52
53
#include <iostream>
#include <string>
#include <vector>

int main()
{
    const int WORDLENGHT = 70; // Maximum allowed characters
    const int MAX_ROW_NUMBER = 10; // Maximum allowed lines of text

    int rows = 0;
    std::cout << "How many rows of text? ";
    std::cin >> rows;
    if(rows > MAX_ROW_NUMBER)
        rows = MAX_ROW_NUMBER;

    // Code if you know std::vector
    std::vector<std::string> list_of_words;
    // code if you prefer C-style arrays
    std::string c_style_list_of_words[MAX_ROW_NUMBER];

    // flush input
    // We need this command because we previously accepted only the integer
    // from std::cin, so the character '\n' is still there
    std::cin.ignore();

    std::string single_word;
    for(int i=0; i<rows; i++)
    {
        std::cout << "Please insert line number " << i << ": ";
        std::getline(std::cin, single_word);
        if(single_word.length() > WORDLENGHT)
        {
            single_word.resize(WORDLENGHT);
        }

        // Code if you know std::vector
        list_of_words.push_back(single_word);
        // code if you prefer C-style arrays
        c_style_list_of_words[i] = single_word;
    }

    std::cout << std::endl;

    for(int i=0; i<rows; i++)
    {
        // Code with std::vector
        std::cout << "\nSentence at vector.at(" << i << "): " 
                  << list_of_words.at(i) << std::endl;
        // Code with C-style arrays
        std::cout << "Sentence at array[" << i << "]: " 
                  << c_style_list_of_words[i] << std::endl;
    }
}


Example of output:
How many rows of text? 3
Please insert line number 0: Inserting line number 0 and testing if input will stop at character number 70 or not.
Please insert line number 1: Now inserting line number 1...                                                       
Please insert line number 2: ...and number 2. 


Sentence at vector.at(0): Inserting line number 0 and testing if input will stop at character nu
Sentence at array[0]: Inserting line number 0 and testing if input will stop at character nu

Sentence at vector.at(1): Now inserting line number 1...
Sentence at array[1]: Now inserting line number 1...

Sentence at vector.at(2): ...and number 2.
Sentence at array[2]: ...and number 2.

Topic archived. No new replies allowed.