What is worng with my line writing code?

My code does not allow me to enter the string I want to copy. The first line is how many times it is copied and the second line is the string to be printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
int main() {
    int n;
    char str[1000];
    cin >> n;
    cout << "\n"
    gets(str);
    for(int a=0;a<n;a++) cout << str;
    return 0;
    
}
Use cin.getline(), newer compilers don't have gets() in <cstdio> (C++11 onwards)
http://www.cplusplus.com/reference/istream/istream/getline/

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
	int n;
	char str[1000];

	cin >> n;
	cout << "\n";
	cin.ignore(); // removes extra '\n' character from stream
	cin.getline(str, 1000);

	for (int a = 0; a < n; a++) cout << str;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.