Modifying this code to read 8 inputs onto one single line?
Feb 27, 2019 at 8:07pm UTC
I am trying to modify this code to receive any amount of input, rather than just 10, but also giving the user a code word like "DONE" to end the program. However, get 8 of those inputs onto a single line rather than just one per line?
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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
using namespace std;
int main()
{
string s;
vector<string>v;
for (int i = 0; i < 10; ++i)
{
cin >> s;
v.push_back(s);
}
for (int i = 0; i < v.size(); ++i)
{
for (int j = 0; j < v[i].size(); ++j)
{
if (islower(v[i][j]))
{
v[i][j] = toupper(v[i][j]);
}
}
}
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << "\n" ;
}
return 0;
}
Feb 27, 2019 at 8:10pm UTC
Any # of input:
1 2 3 4 5 6 7 8 9
vector <string> v;
std::cout << "enter amount: " ;
int amount;
std::cin >> amount;
for (int i = 0; i < amount; i++)
{
cin >> s;
v.push_back(s);
}
8 per line:
1 2 3 4 5 6 7
for (int i = 0; i < v.size(); ++i)
{
cout << v[i];
if (i % 8 == 7)
cout << '\n' ;
}
% (modulus) operator is basically like taking the remainder.
0 % 3 == 0
1 % 3 == 1
2 % 3 == 2 (new line after)
3 % 3 == 0
4 % 3 == 1
5 % 3 == 2 (new line after)
6 % 3 == 0
...
Last edited on Feb 27, 2019 at 8:17pm UTC
Feb 27, 2019 at 8:33pm UTC
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
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;
using namespace std;
int main()
{
string s;
vector<string>v;
std::cout << "Please enter amount needed in string: " ;
int amount;
std::cin >> amount;
for (int i = 0; i < amount; i++)
{
cin >> s;
v.push_back(s);
}
for (int i = 0; i < v.size(); ++i)
{
for (int j = 0; j < v[i].size(); ++j)
{
if (islower(v[i][j]))
{
v[i][j] = toupper(v[i][j]);
}
}
}
for (int i = 0; i < v.size(); ++i)
{
cout << v[i];
if (i % v.size() == v.size() -1)
cout << "\n" ;
}
return 0;
}
Please enter amount needed in string: 3
Running
a
test
RUNNINGATEST
Topic archived. No new replies allowed.