I am trying to read in a sentence to unscramble, however something is going wrong. When I enter no character it'll print out "The sentence is" and "Decoded sentence is", but when I enter one or more characters it'll just sit there and do nothing. I don't think it could be an error with MySentence class because it does not even print "The sentence is".
#include <iostream>
#include <stdio.h>
#include "MySentence.h"
#include "Corpus.h"
#include <string>
using namespace std;
int main() {
Corpus corp;
std::cout << "The proportions are: ";
for(int i = 0; i<26; i++) {
cout << corp.proportion(i+97) <<", ";
}
cout << endl;
cout << "Enter sentence terminated by <ENTER> ";
string s= "";
getline(cin, s);
cout << "The sentence is " << s;
MySentence sent(s);
sent.decode(corp);
cout << endl << "Deoded sentence is: " << sent.sentence;
return 0;
}
As suggested by n.m. try to add an endl at the end of the line
cout << "The sentence is " << s << endl;
since it is possible that the buffer is not being flushed and the problem is in the class MySentence.
An interesting post that might help would be
Buffer flushing: "\n" vs. std::endl
Related
I am working on a small program that takes uppercase letters and converts them to lowercase. I have this accomplished, but when outputting the result it shows before my text when it should be after. How should I go about fixing this?
Thank you.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_word;
cout << "Please enter a word: ";
getline(cin, users_word);
cout << "You entered the word: " << users_word << endl;
char i = 0;
char c = 0;
while (users_word[i])
{
c = users_word[i];
putchar(tolower(c));
i ++;
}
cout << "Your word in lowercase is: " << c << endl;
}
The output is:
Please enter a word: Hello
You entered the word: Hello
helloYour word in lowercase is:
I am trying to figure out how to get "hello" afterwards.
By calling putchar() directly, you are bypassing any buffering that std::cout does internally. You are outputting characters to the terminal before the cout buffer is flushed to the terminal.
Also, even if you were using std::cout instead of putchar(), you are still outputting the lowercase characters before you output "Your word in lowercase is: ". You need to output that message before entering your loop, not outputting it after the loop.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_word;
cout << "Please enter a word: ";
getline(cin, users_word);
cout << "You entered the word: " << users_word << endl;
cout << "Your word in lowercase is: ";
for (string::size_type i = 0; i < users_word.size(); ++i)
{
char c = users_word[i];
//putchar(tolower(c));
cout.put(tolower(c));
// or: cout << (char) tolower(c);
}
cout << endl;
}
Actually the while loop implicitly redirects the lower chars to std::out already.
Thus,
cout << "Your word in lowercase is: " << c << endl;
will appear your lower text in front of Your word in lowercase is:. Just move the cout << "Your word in lowercase is: "; before while statement.
#include <iostream>
#include <string>
using namespace std;
int main()
{
...
cout << "Your word in lowercase is: ";
while (users_word[i])
{
c = users_word[i];
putchar(tolower(c));
i++;
}
cout << endl;
}
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (XXXXX==name1)
cout << adrs1[100] << rsn1[100] << '\n';
else (XXXXX=="exit");
break;
return 0;
}
}
that's my program, and compiling is okay. but when i start the program, it doesn't print any rsn or adrs, it just ends.
I want it to print rsn and adrs when it reads names.
Help me please
There are quite a few errors in your program.
The most important one is that you are trying to write an infinite loop. But it runs exactly once. You need to move your return statement out of the loop.
There is no need for a conditional statement for an else block. You can remove it along with the semi colon.
You're trying to print a character at the index 100 which goes out of bounds.
I don't know what XXXXX is supposed to be. May be you missed pasting the declaration on this website.
At this point, I really suggest picking up a book or trying to debug your code by going step-by-step through your code. It would be more helpful to you at this stage in your learning than this website,
To complete the answer above, the correct program would be:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (strcmp(XXXXX,name1) == 0)
cout << adrs1 << rsn1 << '\n';
else /*(XXXXX=="exit");*/
break;
//return 0;
}
}
You forgot to initilize the name1 variable, you can initialize it using char name1[100] = {};
You cannot directly compare the if (XXXXX==name1), use can use the strncmp function for the same. I will prefer the string class instead of char pointer. Use the following:
if (!strncmp(XXXXX,name1,100))
cout << adrs1 << rsn1 << '\n';
else if (!strncmp(XXXXX,"exit",100))
break;
I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04
I have one string for example, " Today is Monday " if the user types "o", the program must output "Today and Monday" because they contain "o".
My code is this but its not working its only search substring.
#include <iostream>
#include <string.h>
using namespace std;
#define size 100
int main()
{
char str[size];
char searching_string[size];
cout << " Enter String : ";
cin.getline(str,100);
cout << " String : " << str;
cout << " Enter Search String : ";
cin.getline(searching_string,size);
cout << endl << endl;
cout << strstr(str,searching_string);
cin.get();
cin.get();
return 0;
}
Besides of the Quality of this Question.
I think strchr() is what you are looking for.
Link
Hang on.
I am working on an assignment for my C++ class. The following code is given. The directions explain to enter a six character string and observe the results. When I do this, the second user prompt is passed over and the program ends. I am pretty certain the reason for this is that the first cin.getline() is leaving the extra character(s) in the input stream which is messing up the second cin.getline() occurrence. I am to use cin.get, a loop, or both to prevent the extra string characters from interfering with the second cin.getline() function.
Any tips?
#include <iostream>
using namespace std;
int main()
{
char buffer[6];
cout << "Enter five character string: ";
cin.getline(buffer, 6);
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
cout << "Enter another five character string: ";
cin.getline(buffer, 6);
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
return 0;
}
You are right. The newline character stays in the input buffer after the first input.
After the first read try to insert:
cin.ignore(); // to ignore the newline character
or better still:
//discards all input in the standard input stream up to and including the first newline.
cin.ignore(numeric_limits<streamsize>::max(), '\n');
You will have to #include <limits> header for this.
EDIT:
Although using std::string would be much better, following modified code works:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
char buffer[6];
cout << "Enter five character string: ";
for (int i = 0; i < 5; i++)
cin.get(buffer[i]);
buffer[5] = '\0';
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
cout << "Enter another five character string: ";
for (int i = 0; i < 5; i++)
cin.get(buffer[i]);
buffer[5] = '\0';
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl << endl;
cout << "The string you entered was " << buffer << endl;
return 0;
}