Difference between these two? - c++

#include <cctype> // Character testing and conversion
using std::cin;
using std::cout;
using std::endl;
int main() {
char letter = 0; // Store input in here
cout << endl
<< "Enter a letter: "; // Prompt for the input
cin >> letter; // then read a character
if(std::isupper(letter)) { // Test for uppercase letter
cout << "You entered a capital letter."
<< endl;
cout << "Converting to lowercase we get "
<< static_cast<char>(std::tolower(letter)) << endl;
return 0;
}
if(std::islower(letter)) { // Test for lowercase letter
cout << "You entered a small letter."
<< endl;
cout << "Converting to uppercase we get "
<< static_cast<char>(std::toupper(letter)) << endl;
return 0;
}
cout << "You did not enter a letter." << endl;
return 0;
}
Here in this example, what is the difference between using 'std::' if(std::isupper(letter)) { and not using 'std::' if(isupper(letter)) {?
I tried both and they return the same result so I'm not sure what would be the benefit of using 'std::'

Posting namezero user comment:
without the std::, you'll call a function named isupper() from the current scope, and if there isn't any, from the global namespace (::isupper()). Writing std::isupper() refers to a function names isupper() in namespace std

Related

C++ cout formatting

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;
}

compiling is okay but my program doesn't really work

#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;

confusing syntax of cin.get()

I read a book which mentions that cin.get() will keep delimiter in the input stream, thus, the result of a following consecutive calling with the same delimiter is an empty line. So I wrote the following code to test this property and other.
#include <iostream>
using namespace std;
int main()
{
char array[10];
int character;
cin.get(array, 10, 'a');
cout << endl << array << endl;
cout << cin.eof() << endl;
cin.get(array, 10, 'a');
cout << "not ignored: " << array << endl;
cin.ignore();
cin.get(array, 10,'a');
cout << "ignored: " << array << endl;
while((character=cin.get())!=EOF){}
cout << character << endl;
cout << cin.eof() << endl;
}
I then type in "Miami is a city(Enter)" in the terminal, get the following results:
Mi
0
not ignored:
ignored:
-1
0
I don't make sense several points. I didn't input ‘EOF’, but the character holds value of '-1'. I guess it might be that the second cin.get(array, 10, 'a'); get an empty line, it just views it as ‘EOF’? Am I right? If so, it makes sense that no other chars follows "ignored:". But if so, why the last statement print out '0'? Thanks!
From http://en.cppreference.com/w/cpp/io/basic_istream/get
If no characters were extracted, calls setstate(failbit). In any case, if count>0, a null character (CharT() is stored in the next successive location of the array.
Since, no characters were extracted in the second call
cin.get(array, 10, 'a');
failbit was set. You'll have to clear the state before you can read more characters from the string.
Working code:
#include <iostream>
using namespace std;
int main()
{
char array[10];
int character;
cin.get(array, 10, 'a');
cout << endl << array << endl;
cout << cin.eof() << endl;
cin.get(array, 10, 'a');
// failbit is set since no characters were extracted in the
// above call.
cout << "not ignored: " << array << endl;
// Clear the stream
cin.clear();
cin.ignore();
cin.get(array, 10,'a');
cout << "ignored: " << array << endl;
while((character=cin.get())!=EOF){}
cout << character << endl;
cout << cin.eof() << endl;
}

Using cin.get() to discard unwanted characters from the input stream in c++

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;
}

C++ If Else statement Problems

My code contains an If Else statement. I want it to validate a string that a user inputs puts into the script on two things:
The length of the string has to be more than 0
The string has to consist of alphabet characters
Below is my code, I think the error is to do with the length of the string piece of code.
Before Edit:
cout << "Your word: " << szOriginal << "\n";
if szOriginal.length() > 0 and (isalpha(szOriginal))
{
szWord = szOriginal.tolower();
cout << szWord << "\n";
}
else
{
cout << "Please enter a valid word." << endl;
}
Sorry guys, the error produced by Dev-C++ is
expected '(' before "szOriginal"
Link to printscreen: http://gyazo.com/b3f43928072e9c2a5a8a712a9030364d
After Edit with variable declaration and such:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
string original;
cout << "Welcome to the English to Pig Latin translator!\n";
cout << "Type a word you wish to translate:\n";
cin >> original;
cout << "Your word: " << original << "\n";
if (original.length() > 0 && (isalpha(original)) )
{
word = original.tolower();
cout << Word << "\n";
}
else
{
cout << "Please enter a valid word." << endl;
}
system("PAUSE");
return 0;
}
So sorry for this bad post!
if ( [...] ) - you're missing the brackets.
Also, it's && instead of and.
I think you are coming from a python background
cout << "Your word: " << szOriginal << "\n";
if (szOriginal.length() > 0 && (isalpha(szOriginal)) )
{
szWord = szOriginal.tolower();
cout << szWord << "\n";
}
else
{
cout << "Please enter a valid word." << endl;
}
you need ( ) around the if condition, and use && for logical and
EDIT
you are using szOriginal as a char when you say isalpha(szOriginal)
and as an object when you say szWord = szOriginal.tolower();
so you need to look into that also