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.
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 5 years ago.
Improve this question
I am very new at coding and I just had a quick question.
How would you find the nth character from a string in an array for c++?
so for example
{"Hey you" "echo" "lake" "lend" "degree"}, n=0
would equal "hello"
thanks! I'm not going to ask for the full code but just tips on where to get started.
Here are some examples:
unsigned int n = 3;
static const char hello[] = "hello";
cout << hello[n] << "\n";
const std::string apple = "apple";
cout << apple[n] << "\n";
static const char * many[] = {"These", "are", "many", "strings"};
cout << many[n][n] << "\n";
cout << many[n] << "\n";
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
I know how to use a string to calculate the number of characters, but I'm not sure how to use a function to do that. have to use CSTRING. THANK YOU ALL
#include <cstring>
char a[10];
cout << "Please enter anything: ";
cin.getline(a,10);
cout << "You type " << strlen(a) << " letters long"<<endl;
You're probably looking for std::string since you're question mentioned C++ and not only C.
include <string>
std::string myString = "Something";
size_t stringLength = myString.size();
It's simple. Just type your code inside a function()
int stringlengthfunction()
{
char str[80];
int i;
cout<<"\n enter string:";
cin.getline(str,80);
int n=strlen(str);
cout<<"\n lenght is:"<<n;
getch();
return 0;
}
or pass your string as a parameter to the function
int stringlengthfunction(string str)
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
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 9 years ago.
Improve this question
I want to declare
int x = it must be more or equal to 1 but less or equal to 100;
How can I do it? I dont want to use if condition, Im looking for something short and clear, if possible.
The x number is input, so program should accept only numbers in this limit.
It seems that you're looking to error check on initialization.
If I were you I'd do something along the lines of.
int x;
cout << "Enter a value: " << flush;
cin >> x;
while(!((x>=1)&&(x<=100))) {
cout << "Try Again: " << flush;
cin >> x;
}