How to use string with phrases that contain spaces [closed] - 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 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];

Related

How can i make cin.ignore to take any non integer value to be an delimiter [closed]

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 months ago.
Improve this question
#include <iostream>
#include <string>
int main(){
int a, b;
std::cin >> a;
std::cin.ignore(3, '/');
std::cin >> b;
std::cout << a << " " << b;
return 0;
}
How can I change my code so that the separators do not have to be Slash characters “/” but any separator that is not an integer number.
You can't use ignore for that.
Instead use a loop to peek at and fetch the next character until the character matches your condition to stop "ignoring" character.
The characters you read that you want to "ignore", just don't do anything with them.

Changing first and last letter of a 6 letter word in C++ [closed]

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++?

c++ Send the string to a function to determine how long is the string [closed]

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)

Catch an exception when user enters integer value in character array [closed]

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

Newbie in c++ is stuck [closed]

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 started learning c++ last week and ive finally learned enough to try and stand on my own feets. well guess what i have a problem. the program im trying to make will ask for a file already existing or creates a new one if the name isnt found, and places information in the file lines. when you type -1 you close the program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string x;
string Input;
int line=0;
cout << "Please enter the name of the file with the file type" << endl;
cin >> x;
ofstream SelectedFile;
SelectedFile.open(x);
while(Input != "-1"){
cout << "Enter the content of the " << line <<" line, or type -1 to quit." << endl;
cin >> Input;
line++;
}
SelectedFile.close();
}
I guess you're having a compile error because std::ofstream::open does not take an std::string as an argument in C++98 standard. Try this one:
SelectedFile.open(x.c_str());
Or compile with C++11 support.
Update: Where you write things into file? I think you forgot to write that part
SelectedFile << line << std::endl ;