#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Enter " << n << "names";
for(int i=0; i<n; i++)
{
system("pause>0");
return 0;
}
This is my unfinished code. I'm required to enter a number then it will ask me to enter n names. and after entering the names the program should sort the names alphabetically. How will I do that in Looping? I'm very confused in the looping part. Yeah, I know what I will code when I'm finished in looping. I'm just confused and having problems in this part. Thanks in Advance!
Here's an STL version of what you're trying to do:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <algorithm>
int main() {
std::vector<std::string> names;
int num = 0;
std::cout << "Please enter a number: ";
std::cin >> num;
std::cout << "\n";
std::string name;
for (int i = 0; i < num; ++i) {
std::cout << "Please enter name(" << (i+1) << "): ";
std::cin >> name;
names.push_back(name);
}
//sort the vector:
std::sort(names.begin(), names.end());
std::cout << "The sorted names are: \n";
for (int i=0; i<num; ++i) {
std::cout << names[i] << "\n";
}
return 0;
}
However, this version is a case-sensitive sort, so whether or not that performs to your requirements could be problematic. So, a possible next step to get closer to case-insensitive sorting is to use this bit of code before the vector is sorted:
//transform the vector of strings into lowercase for case-insensitive comparison
for (std::vector<std::string>::iterator it=names.begin(); it != names.end(); ++it) {
name = *it;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
*it = name;
}
The only caveat with this method is that all your string will be converted to lowercase, though.
REFERENCES:
https://stackoverflow.com/a/688068/866930
How to convert std::string to lower case?
Related
I may just be having a brain fart, upon doing the bubble sort algorithm, of course, I encountered a problem. Any help will be golden. The problem is to do with the "float numbers[n];" and the "std:string c;".
int n = 0;
cout << "Enter value of N: ";
cin >> n;
float numbers[n];
cout << "You will enter " << n << " numbers." << endl;
for (int i = 0; i < n; i++) {
std:string c;
cin >> c;
numbers[i] = atof(c.c_str());
}
It is not exactly clear to me (or us) what your problem exactly is. But in any case, std:string has to be replaced by std::string. Double colon sign, indicating that string lies in the std namespace.
Besides, it is much more idiomatic, and more portable and less error-prone, if you replace the C-style array by a proper std::vector object.
This code compiles OK:
#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int n = 0;
cout << "Enter value of N: ";
cin >> n;
std::vector<float> numbers(n);
cout << "You will enter " << n << " numbers." << endl;
for (int i = 0; i < n; i++) {
std::string c;
cin >> c;
numbers[i] = stof(c);
}
// etc...
return EXIT_SUCCESS;
}
So, while practicing strings i came across this question that gave me, "n" number of strings and it asked me to output strings in increasing alphabetical order.
Example :
Input>>
4 // number of string
abcdef ghi // string 1
ccdef // string 2
bcdcas // string 3
xcxvb // string 4
vxzxz // string 5
This will output only strings 1,2,4 because we have to print string in an increasing alphabetical way.
string 1 < string 2 < string 4.
(string 3 is smaller than string 2, and hence the output)
So i coded the problem without using string array and it worked, but when i applied the same approach the output was not correct.
Maybe i don't know something about string array that you guys can help me with.
Here is the code for you guys :
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
string array[n];
cin.ignore();
for(int i=0; i<n;i++){
getline(cin , array[i]);
}
cout << array[0] << endl;
string maximum;
for(int i = 0; i<n; i++){
maximum = array[0];
if(array[i] > maximum){
maximum = array[i];
cout << maximum << endl;
}
}
}
Here is the code that worked without any problems:
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
string text;
cin.ignore();
string max = "";
for(int i=0; i<n;i++){
getline(cin , text);
if(text>max){
max = text;
cout << text << endl;
}
}
}
I've used your working code as a starting point. Try to avoid c-style arrays and use one of the C++ containers (like std::vector) instead.
#include <iostream>
#include <string>
#include <vector>
int main()
{
int n;
std::string text;
std::vector<std::string> array;
std::cout << "Enter number of strings: ";
std::cin >> n;
std::cin.ignore();
for(int i=1; i<=n;i++) {
std::cout << "Enter string " << i << ": ";
std::getline(std::cin, text);
// check if we already have stored anything in the array and
// then check if text is <= than the last element in the array.
// if so, continue will skip to the next iteration in the for-loop
if(array.size()>0 && text<=array.back()) continue;
std::cout << "saved " << text << "\n";
array.emplace_back(std::move(text));
}
std::cout << "All saved strings:\n";
for(auto& s : array) {
std::cout << s << "\n";
}
}
Below code failed:
#include <iostream>
#include <string>
using namespace std;
int main(){
int a, b;
cout << "how many input you want to give ? ";
cin >> a;
b = a - 1;
string str[b];
for(int i = 0; i <= b; i++){
cout << "Enter a string: ";
getline(cin, str[i]);
}
for(int k = 0; k < a; k++){
cout << "You entered: " << str[k] << endl;
}
return 0;
}
But if I fix the value of 'int a' then the code is running. please help.
Arrays must have a constant size at compile-time so to create an array with dynamic size you can create it on the heap using keyword new and delete[] to free up memory.
Also what is the point in:
cin >> a;
b = a - 1; //?
You can easily do it like this:
int n;
cout << "how many input you want to give ? ";
cin >> n;
string* str = new string[n];
for(int i = 0; i < n; i++){
cout << "Enter a string: ";
getline(cin, str[i]);
}
for(int k = 0; k < n; k++){
cout << "You entered: " << str[k] << endl;
}
Don't forget to clean when you're done:
delete[] str;
You'll want to clear the input buffer, the newline from "How many input you want to give ?" is being fed into the first "Enter a string:".
Add this line after cin>>a;
cin.ignore(INT_MAX, '\n');
Use a vector to store the input.
#include <vector>
#include <string>
#include <iostream>
using names pace std;
int main ()
{
vector <string> input;
string tmp;
while (getline(cin, tmp))
input.push_back(tmp));
for(auto s : input)
cout << s << '\n';
}
How would I find the sum of the elements in a vector that was inputted by a user? I tried searching for a method to do so everywhere online but couldn't really find one online that explained it really well, nor was it explained in class too much unfortunately.
So I basically have the vectors inputted by a user here, but I have no idea how to use it to take the sum of it? (printvector is only there because I have to present what the user put in to the user before telling the user the sum)
#include <iostream>
#include <vector>
using namespace std;
void fillVector(vector<int>&);
void printVector(const vector<int>&);
int main()
{
vector<int> VectorQuantities;
fillVector(VectorQuantities);
printVector(VectorQuantities);
return 0;
}
void fillVector(vector<int>& newVectorQuantities)
{
cout << "Type in a list of numbers, and type in -1 as the last number when you are finished: ";
int input;
cin >> input;
while (input != -1) {
newVectorQuantities.push_back(input);
cin >> input;
}
cout << endl;
}
void printVector(const vector<int>& newVectorQuantities) {
cout << "Vector: ";
for (unsigned int i=0; i < newVectorQuantities.size(); i++) {
cout << newVectorQuantities[i] << " ";
}
cout << endl;
}
You can use std::accumulate().
#include <algorithm>
std::vector<int> vec = ...;
int vecSum = std::accumulate(std::begin(vec), std::end(vec), 0);
The accumulate() function is really just a left fold, and by default it uses the + function to combine elements.
Try this:
void printSum(const vector<int>& newVectorQuantities) {
cout << "Sum: ";
int sum = 0;
for (unsigned int i=0; i < newVectorQuantities.size(); i++) {
sum = sum + newVectorQuantities[i];
}
cout << sum << " ";
}
(Using your style for the function, not modern C++.)
I created a program that asks the user to enter 5 names which is recorded into a vector of strings. Afterwards the program is supposed to grab the first and last letters of each name and output them. My program compiles fine however after entering the names I get no output from the program.
Can anyone help me correct this issue so that it prints the first and last characters of each name entered?
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> names;
char first_letter;
char last_letter;
string name;
int n = 0;
for (int i =0; i < 5; i++)
{
cout << " Please enter a name: ";
cin >> name;
names.push_back(name);
}
if ( !names[n].empty() )
{
first_letter = *names[n].begin();
last_letter = *names[n].rbegin();
cout << first_letter << " " << last_letter << endl;
n++;
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> names;
char first_letter;
char last_letter;
string name;
int n = 0;
for (int i =0; i < 5; i++)
{
cout << " Please enter a name: ";
cin >> name;
names.push_back(name);
}
vector<string>::iterator itr = names.begin();
for(;itr!=names.end();itr++)
{
first_letter = *itr[n].begin();
last_letter = *itr[n].rbegin();
cout << first_letter << " " << last_letter << endl;
}
return 0;
}
You have entered it as a if statement. Change it to a while loop
while ( !names[n].empty() )
{
first_letter = *names[n].begin();
last_letter = *names[n].rbegin();
std::cout << first_letter << " " << last_letter << endl;
n++;
}