+ 1
How can i take input of variable number of Strings in c++, if not possible in c++ then tell me in other language.
I joined a competitive Coding challenge, there, they said to make a program by inputting 2 integer values n and m i.e., int n,m; then, input n Names none of which is more than m characters longs. then output all those with greetings. for e.g., input: 4 7 Nilesh Nick Jackson Samson Output: Hello Nilesh Hello Nick Hello Jackson Hello Samson
12 Réponses
+ 1
Nilesh here it is, I put comments in code for you to understand.
#include <string>
#include <iostream>
int main()
{
int n, m;
// Get number of names, and max name length
std::cin >> n >> m;
// Declare <n> string array
// Start getting name inputs
std::string names[n];
for(unsigned int i {0}; i < n; ++i)
{
std::cin >> names[i];
// If length more than <m> characters
// crop the name to <m> characters
if(names[i].length() > m)
names[i].resize(m);
std::cout << "Hello " << names[i] << "\n";
}
return 0;
}
+ 1
Where is your code in attempt to solve the challenge? you need to have tried it yourself first, then if you have problems ask the community with your code attached in question.
+ 1
Nilesh please explain, what to do if user input name longer than m characters, should we crop it to maximum of m characters?. Also is it really necessary to use char array, as I see you include string header, we can use string instead of char array if it were allowed, and be more simple too.
+ 1
oh yeah, that worked perfectly, thanks alot mate!
+ 1
Nilesh here's an alternative to the first solution, expanded from a question in SO. This way no need for cropping the name because the size of input is adjusted in time:
#include <string>
#include <iostream>
#include <iomanip> // std::setw
// Limit string input length using
// stream manipulator std::setw
// Found here:
// https://stackoverflow.com/questions/10770870/how-do-i-limit-the-number-of-characters-entered-via-cin
int main()
{
int n, m;
std::cin >> n >> m;
std::string names[n];
for(unsigned int i {0}; i < n; ++i)
{
// Set size of input stream buffer
std::cin >> std::setw(m) >> names[i];
std::cout << names[i] << "*" << std::endl ;
// Discard excessive data
std::cin.ignore();
}
}
0
I have no idea how to input Variable number of strings, I did make a program but it's not how I want it to be
0
my attempt:
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
int i,j,n,m;
cin>>n>>m;
char a[n][m];
for(i=0; i<n; i++)
{ for (j=0; j<m; j++)
{cin>>a[i][j];}
cout<<endl; }
cout<<"\n \n ";
for(i=0;i<n;i++)
{for(j=0;j<m;j++)
{cout<<a[i][j];}
cout<<endl;
}
return 0;
getch();
}
0
name longer than m characters are not allowed at all, and I tried to use string array but I was getting some compiling errors so I thought String array can't be made.
actually I wanted to use string array itself but idk how to
0
Okay Nilesh, because you have showed good will to try I will also try to help you, let me get back to you later Okay buddy ...
0
thanks alot mahn, would be of great help :D 🙏
0
Nilesh alright, that's what we're here for, help friends learning, anyways, good luck with that competition : )
0
thanks brother :3
Btw in the same question, is there any way to not accept characters more than 'm' in one single string? just in case I ever need to use it