Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So my question is if I have a character array, I'm only allowed to enter characters in it. If I enter integer with character let's suppose "abc123" then this shouldn't be allowed. How to do I do this?
Use std::none_of, along with isdigit:
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
int main()
{
std::string test = "abc123";
if ( std::none_of(test.begin(), test.end(), ::isdigit))
std::cout << "All good\n";
else
std::cout << "You've entered an integer\n";
// Try with good data
test = "abcdef";
if ( std::none_of(test.begin(), test.end(), ::isdigit))
std::cout << "All good\n";
else
std::cout << "You've entered an integer\n";
}
Live Example
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
INPUT STRING
ABCE
INPUT CHAR
D
OUTPUT STRING
ABCDE
It should run for all sizes , and it should be a standard procedure ie run for all the cases.
Can anyone Help me with this? Any help would be appreciated.
You should try the std::string::insert(..). Check out the documentation
#include <iostream>
#include <string>
int main() {
std::string str("ABCE");
std::cout << str << std::endl; // ABCE
str.insert(3, 1, 'D');
std::cout << str << std::endl; // ABCDE
return -1;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is there any other method to change the first letter and last letter of a 6 letter word?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
cout<< "\nEnter a 6 letter word or numbers: ";
cin>>word;
word[0]++;
word[5]++;
cout << "Result: " << word << endl;
return 0;
}
you could also use string::replace but it is a pretty roundabout way compared to replacing using the subscript operator.
something like
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
cout<< "\nEnter a 6 letter word or numbers: ";
cin>>word;
cout<<word;
string repl = "x";
word.replace(5,1, repl); //replace the 5th position with 1
//character using string repl
cout << "Result: " << word << endl;
return 0;
}
Take a look at http://cplusplus.com/reference/string/string/replace/
C++, best way to change a string at a particular index
How to replace one char by another using std::string in C++?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
please find below program, the sort output is not proper.
string pru[] = { "ruthvi$p", "uthvi$pr", "thvi$pru", "hvi$prut", "vi$pruth", "i$pruthv", "$pruthvi", "pruthvi$" };
sort(pru, pru + 8, cmp);
for(int i = 0; i < 8; i++)
cout << pru[i] << " ";
output is
$pruthvi hvi$prut i$pruthv pruthvi$ thvi$pru ruthvi$p uthvi$pr vi$pruth
mistake "thvi$pru" is before "ruthvi$p"
It is not clear how cmp is defined but you can use this code and you will get the expected result.:)
#include <string>
#include <iterator>
#include <algorithm>
//...
std::string pru[] =
{
"ruthvi$p",
"uthvi$pr",
"thvi$pru",
"hvi$prut",
"vi$pruth",
"i$pruthv",
"$pruthvi",
"pruthvi$"
};
std::sort(std::begin(pru), std::end(pru));
for (const auto &s : pru) std::cout << s << std::endl;
The output is
$pruthvi
hvi$prut
i$pruthv
pruthvi$
ruthvi$p
thvi$pru
uthvi$pr
vi$pruth
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I used cin.getline after cin.ignore() but I am getting an error saying unassigned int... Not sure what to do or what is wrong. Any suggestions?
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string phras;
cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
cin.ignore();
cin.getline(phras, sizeof(phras));
cout << " The phrase is: " << phras << endl;
cout << endl;
return 0;
}
UPDATE
I changed cin.getline(phras, sizeof(phras));
to getline(cin,phras)
Problem solved! Thanks for the help everyone!
The problem is that
char letter[1];
isn't large enough. If a C string is to hold up to N characters, it needs to be declared char letter[N+1] to allow room for the null terminator character. So if the user is going to type a single character, it needs to be:
char letter[2];
As a result, you're getting undefined behavior when cin >> letter writes 2 characters into the array that only has room for 1.
Similarly, if the user is allowed to type a 10-letter word, it should be:
char word[11];
and it should be:
char phrase[31];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Im newbie in c++ programming. How can I do something like this?..
int question1;
question1: "What is your name?";
to set the text value in integer?
#include <string>
#include <iostream>
int main( )
{
std::string name;
std::cout << "What is your name?: " << std::endl;
std::cin >> name;
std::cout << "Your Name: " << name << std::endl;
std::cin.get( );
return 0;
}
Simply read the input in as string.