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 :
#include <iostream>
#include <conio.h>
usingnamespace 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;
}
#include <iostream>
#include <string>
#include <vector>
int main()
{
constint WORDLENGHT = 70; // Maximum allowed characters
constint 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.