This question already has answers here:
std::cin input with spaces?
(8 answers)
Closed 8 years ago.
The code is not giving desired output
when I type in a string example "Ben Parker", the output is "Goodmorning, Ben" and not the entire name("Ben Parker") what seems to be the problem?
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main() {
char your_name[20];
std::cout << "Enter your name: ";
std::cin >> your_name;
std::cout << "Goodmorning, ";
std::cout.write (your_name, strlen(your_name)) << std::endl;
return 0;
}
SOLUTION
This was a very old question when I just began programming.
The entire character array can be read and printed with a for loop, or better a string type variable can be used, since it is C++.
using string your_name; seems to fix the problem, which can be then printed with a simple std::cout << your_name << endl;
You probably put a space in between "Ben" and "Parker" in input. This would cause the cin logic to believe it had an answer after seeing the space following "Ben". You will probably want to read an entire line at a time to get past that problem. See this page for an example.
Related
This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Closed 7 years ago.
Hey guys I'm new here and a programming noob so bear with me here.
This is for my C++ class which sadly my teacher is terrible at teaching so many things confuse me so I need some help here.
We have a lab that is called 'Reverse Sentence' and this is what it wants In this lab.
Write the function "ReverseSentence" that takes a string parameter and changes it, by reversing it.
For example:
INPUT: the first test
OUTPUT: tset tsrif eht
The function must not use an extra string, but must reverse the elements of the input string.
#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
/* Body of Function Here */
}
int main(){
string inputSentence;
cout << "Input your sentence: ";
getline(cin, inputSentence);
cout << endl;
ReverseSentence(inputSentence);
cout << "Reversed Sentence:" << endl;
cout << inputSentence << endl;
return 0;
}
Can someone please help me what function is because I'm having trouble with it.
Just use std::reverse:
void ReverseSentence( string& inputSentence){
std::reverse(inputSentence.begin(), inputSentence.end());
}
Half of the cycle and swap.
#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
for (int i = 0; i < s.size()/2; ++i)
std::swap(s[i], s[s.size() - i - 1]);
}
This question already has answers here:
std::string length() and size() member functions
(4 answers)
Closed 7 years ago.
In this exercise from C++ Primer strings chapter, the directions read:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger. Now, change the
program to report whether the strings have the same length, and if
not, report which is longer.
To which I wrote:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
/* Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer. */
int main()
{
string line;
string word;
getline(cin, line);
getline(cin, word);
if (line == word) {
cout << "String are equal." << endl;
}
else {
if (line > word)
cout << line << " is longer." << endl;
else {
cout << word << " is longer." << endl;
}
}
return 0;
}
Which seemed to work for the problem. Now for the first example of:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I changed the comparisons to have .size(), and for this one:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I removed the .size() for the comparisons. I printed out both size and .length() and I got the same answers, so I was wondering if I am misinterpreting this problem or was it to show that length and size are really the same thing?
Both string::size and string::length are synonyms and return the exact same value.
Reference http://www.cplusplus.com/reference/string/string/length/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am a beginner to C++ and have been pondering this problem for quite a while, but I'm finding myself unable to come up with a solution and was hoping I could find some direction here.
I have an input file that will contain any number of ASCII characters (ex: hello, world; lorem ipsum; etc.). My program will read this file and count the frequency of each ASCII character, outputting the end counts when EOF is reached. I believe I need to use array[128] for the counters, but besides that, I'm totally stuck.
Here's what I have so far (it's not much and only reads the characters from the file):
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(void)
{
ifstream inputFile;
string infile;
char ch;
//char ascii;
//int asciiArray[128] = {0};
// Gets input filename from user, checks to make sure it can be opened, and then
// gets output filename from user.
cout << "Please enter your input filename: ";
cin >> infile;
inputFile.open(infile.c_str());
if (inputFile.fail())
{
cout << "Input file could not be opened. Try again." << endl;
exit(0);
}
// Gets the first character of the input file.
inputFile.get(ch);
while(!inputFile.eof())
{
inputFile.get(ch);
}
// Closes the input file
inputFile.close();
return 0;
}
Any direction or help would be greatly appreciated. I have a feeling I will need to use pointers to solve this...but I've just barely started covering pointers, so I'm very confused. Thanks!
Edit: I removed some variables and it's working now, looks like I forgot them there while I was brainstorming. Sorry for leaving it unworking and not mentioning why; I won't do that again!
You should write your loop as:
while(inputFile >> ascii)
{
asciiArray[ascii]++;
}
Note that I don't directly check for eof in the loop condition since that's almost always wrong.
Also you should be sure that your file is indeed written with ascii characters only. Since any character outside the ascii range would result in an out of bounds access to the asciiArray.
In regular Ascii you have 128 chars... of which each char can be evaluated as an int.
That is the key in solving this puzzle.
Just remember you have 128 possible chars, an array with 128 values, and each char represents a number from 0-127.
Also recall that you can do stuff like this:
int i = 97;
char a = i;
char b = a + 1;
cout << (int)i << (int)a << (int)b << endl;
// 979798
cout << (char )i << (char )a << (char )b << endl;
// aab
cout << i << a << b << endl;
// 97ab
As far as pointers go, the only way I would see them as being used is if you used pointer notation instead of array notation while manipulating your variable asciiArray
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am quite noob with c and c++, and I am stucked trying to read a user input delimited between double quotes for a program I have to deliver to my algorithm class.
The entry would be in this form: "something like this, with spaces, and delimited by this two double quotes".
What I need to get from that is the string ( char * ) contained between the delimiters.
Unfortunatelly I have been trying without luck to solve this small issue...
The development environment is a virtualized Windows 7 and the ide (both are requirements from the teacher) is DEVC++
Anyone could give a hint or help me out? I am stucked with this and I am running out of time.
thanks in advance!
Assuming you have a stream where the current character is a double quote, you can just
ignore() the current character.
getline() using '"' as the delimiter.
Here is code which skips leading space, verifies that the next character is a '"' and, if so, reads the value into str:
std::string str;
if ((in >> std::ws).peek() == '"' && std::getline(in.ignore(), str, '"')) {
std::cout << "received \"" << str << "\"\n";
}
If I understood the question correctly, then the following suits you. This approach will eliminate every punctuation.
#include <string>
#include <algorithm>
#include <iostream>
int main ()
{
std::string input ;
std::cout << "Please, enter data: ";
std::getline (std::cin,input);
input.erase( remove_if(input.begin(), input.end(), &ispunct), input.end());
std::cout << input << std::endl;
std::cin.get();
return 0;
}
This is the result.
>Please, enter data: There' ?are numerous issues.
There are numerous issues
This approach is exactly what you are looking for by using strtok
#include <stdio.h>
#include <iostream>
int main()
{
char sentence[] = "\"something like this, with spaces, and delimited by this two double quotes\"";
char * word;
std::cout << "Your sentence:\n " << sentence << std::endl;
word = strtok (sentence,"\"");
std::cout << "Result:\n " << word << std::endl;
return 0;
}
The result
Your sentence:
"something like this, with spaces, and delimited by this two double quotes"
Result:
something like this, with spaces, and delimited by this two double quotes
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
C++ cin whitespace question
I'm having problem trying to understand this piece of code. I'd like to apologize if this question has already been answered but I didn't find it anywhere. I'm a beginner and its a very basic code. The problem is >> operator stops reading when the first white space
character is encountered but why is it in this case it outputs the complete input string even if we have white spaces in our string. It outputs every word of the string in separate lines. How is it that cin>>x can take input even after the white space? Plz help me out with the functioning of this code. Thanks in advance.
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::string;
using std::endl;
int main()
{
string s;
while (cin >> s)
cout << s << endl;
return 0;
}
The problem is >> operator stops reading when the first white space
character is encountered but why is it in this case it outputs the
complete input string even if we have white spaces in our string
Because you're using it in a loop. So each time around cin eats a word which you print and discards whitespace. The fact that you're printing a newline after each word means you don't expect to see whitespace - and in fact s contains none.
A simple way to test this would be to print:
cout << s << "$";
However the most interesting characteristic of the code is how the while tests the istream returned by << which in a boolean context yields exactly what you want: whether the input is done or not.
Using the input operator on a stream by default stops at whitespace, and skip leading whitespace. So since you reading in a loop, it skips whitespace while reading all "words" you input, and then print it inside the loop with a trailing newline.
You probably ignored the fact that whenever you are entering a new string ( after a white space character i.e. a newline, tab or blank-space ) it is being re-assigned to string s in the while loop condition. To verify this you can simply do something like :
int i=1;
while (cin >> s)
cout << i++ << ": " << s << endl;
instead of :
while (cin >> s)
cout << s << endl;
Run this and everything would be crystal clear :)