So, I'm trying to make a basic C++ program that converts Base 10 -> Base 16
while(true){
int input;
cout << "Enter the base 10 number\n";
cin >> input;
cout << "The hex form of " << input << " is: ";
int decimal = input;
cout << hex << uppercase << decimal << "\n";
decimal = NULL;
input = NULL;
Sleep(3000);
}
And on the first run through it works. For example, if I enter 7331 I get:
The hex form of 7331 is 1CA3
But if I try it a second time (with the same input) I get:
The hex form of 1CA3 is 1CA3
I can try this with any integer input and it works fine for the first run through but after that it puts the base 16 number in twice.
You need to reset your stream. When you apply std::hex to std::cout, it applies permanently to the stream (std::cout), and you need to reset it. You can simply change your program to this:
cout << "The hex form of " << std::dec << input << " is: ";
your FIX:
cout << "The hex form of "<< dec << input << " is: ";
You could even shorten your code:
while (true)
{
int input;
cout << "Enter the base 10 number: ";
cin >> input;
cout << "The hex form of " << dec << input << " is: ";
cout << hex << uppercase << input << "\n";
Sleep(3000);
}
Related
My program runs the first For loop correctly then skips the Cin's on the 2nd and 3rd cycle. Then when the loop is finished it goes on to calculate the BMI of the first index [0] and does this correctly and gives the right answer but then nothing for the index's 1 and [2] because no information was inputted because the cin's were skipped.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct Patient
{
double height;
double weight;
int age;
bool isMale;
};
int main()
{
Patient Patients[3];
for (int i = 0; i < 3; i++) {
cout << "Patient "<< i << " Height: ";
cin >> Patients[i].height;
cout << "Patient " << i << " Weight: ";
cin >> Patients[i].weight;
cout << "Patient " << i << " Age: ";
cin >> Patients[i].age;
cout << "Is Patient " << i << " Male True or False: ";
cin >> Patients[i].isMale;
cout << endl << endl;
}
cout << endl << endl;
for (int i = 0; i < 3; i++) {
float BMI = Patients[i].weight / (Patients[i].height *Patients[i].height);
cout << "Patient " << i << " Has A BMI of: " << BMI << endl << endl;
}
return 0;
}
This is the console where you can see after the first loop all the cin's are skipped but the first loop was correctly stored as it couted the BMI of the first index:
You see that you are having an error at the end of the loop. You can see from iterations 2 and 3 that cin is not behaving the same way each time. There are a couple of error state flags that come from ios that will help you see what's going wrong here. See iso::good for details. If you add those checks:
for (int i = 0; i < 3; i++) {
cout << "Patient " << i << " Height: ";
cin >> Patients[i].height;
cout << "Patient " << i << " Weight: ";
cin >> Patients[i].weight;
cout << "Patient " << i << " Age: ";
cin >> Patients[i].age;
cout << "Is Patient " << i << " Male True or False: ";
cin >> Patients[i].isMale;
cout << cin.good() << '\n';
cout << cin.eof() << '\n';
cout << cin.fail() << '\n';
cout << cin.bad() << '\n';
cout << endl << endl;
}
What you will see if that cin is no longer good, it is not eof it is fail, and it is not bad. While the fail bit is set, cin will not work. Hence you see the result. Looking at the chart in the link you see this:
Logical error on i/o operation
You were preforming an i/o operation of inserting "true" into a bool. The word true is probably stored as a character array or string, not a boolean. How should cin convert this to a boolean? You need to trap your input and convert it into a bool or switch to use an input that can be explicitly converted into a bool.
For example:
cout << "Is Patient " << i << " Male? (1 for Male, 0 for Female):";
cin >> Patients[i].isMale;
In this case the cin recognizes 1 and 0 as integers and can convert an integer into a boolean. 0 is false, everything else is true. Another option is to let the library do it and use boolalpha. You can read about it here.
This shows a larger issue. What happens if I write "two point five" as the answer to height? In this case we can assume some intelligence on the part of the user, but thinking about things like this will help write more robust code in the future.
You can fix your program in two ways.
Just input "0" or "1" in the male/female question instead of "true" or "false".
Change this line and continue to input "true" or "false":
cin >> boolalpha >> Patients[i].isMale;
Sources:
Cin and Boolean input
http://www.cplusplus.com/reference/ios/boolalpha/
Okay, I am trying to use the code:
getline(cin, phrase);
When I compile I get the error:
no matching function for call to 'getline'
Here is the full code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
cout << "Challenge 1\n" << "Kaitlin Stevers\n" << "Characters and Strings" << endl;
cout << endl;
cout << endl;
char letter[2];
cout << "Please enter a letter: " << endl;
cin >> letter;
cout << "You entered: " << letter << endl;
char word[5];
cout << "Please enter a word up to 5 characters long: " << endl;
cin >> word;
cout << "The word you entered is: " << word << endl;
char phrase[100];
cout << "Please enter a phrase up to 99 characters long: " << endl;
getline(cin, phrase);
cout << "The phrase you entered is: " << phrase << endl;
string lettero;
cout << "Enter one letter: " << endl;
cin >> lettero;
cout << "The letter you entered is: " << lettero << endl;
string wordo;
cout << "Please enter a word: " << endl;
cin >> wordo;
cout << "The word you entered is: " << wordo << endl;
string phraseo;
cout << "Please enter five words: " << endl;
getline(cin, phraseo);
cout << "The words you entered are: " << phraseo << endl;
return 0;
}
'no matching function call for getline', cause getline takes a string not a char[] as argument. See cin.getline() if you absolutely want to pass a cha[] as argument.
As you see here.
This getline(cin, string) function accepts a string.
Although, there is also an instruction you can use to put the line into a char array like so:
char phrase[99];
cin.getline (phrase,99);
Or you could also get the input into a string, then convert it to a char array :
string temp = "";
cin >> temp;
char phrase[99];
strcpy(phrase, temp.c_str());
Hi I want to use the same letter for an int and cin keyboard input so when I enter in the new number it changes the number in the cell when I enter the score in with the keyboard sample code take into account i'm still a beginner and still learning:
int h = 0;
cout << " _______________________" << endl;
cout << "|chelsea fc |"<< h << "|" << endl;
cout << "|___________|__________|" << endl;
string h = "";
cout << "Type here to add score to table" << endl;
getline(cin, h);
cout << "You added the score " << h << " to the table" << endl;
If I understand you correctly, you want something like this:
int score = 0;
cout << " _______________________" << endl;
cout << "|chelsea fc |"<< score << "|" << endl;
cout << "|___________|__________|" << endl;
cout << "Type here to add score to table" << endl;
cin >> score;
cout << "You added the score " << score << " to the table" << endl;
Remember that when reading numbers and strings, using the input operator >> reads and discards leading white-space in the input, so even if there is a newline in the input buffer after the input of the score, if you attempt to read a new number or a string that newline will simply be ignored.
Try to write a function that outputs the score table based on an integer. Something like this:
void write_table(int h) {
cout << " _______________________" << endl;
cout << "|chelsea fc |"<< h << "|" << endl;
cout << "|___________|__________|" << endl;
}
Call that function after you ask the user for input.
How do you check for non-numeric input using C++? I am using cin to read in a float value, and I want to check if non-numerical input is entered via stdin. I have tried to use scanf using the %d designator, but my output was corrupted. When using cin, I get the correct format, but when I enter, a string such as "dsffsw", I get an infinite loop.
The commented code was my attempt to capture the float, and type cast it as string, and check if it is a valid float, but the check always comes up false.
I have tried using other methods I have found on the message boards, but they want to use scanf in C and not cin in C++. How do you do this in C++? Or in C if it is not feasible.
while (!flag) {
cout << "Enter amount:" << endl;
cin >> amount;
cout << "BEGIN The amount you entered is: " << strtod(&end,&pend) << endl;
//if (!strtod(((const char *)&amount), NULL)) {
// cout << "This is not a float!" << endl;
// cout << "i = " << strtod(((const char *)&amount), NULL) << endl;
// //amount = 0.0;
//}
change = (int) ceil(amount * 100);
cout << "change = " << change << endl;
cout << "100s= " << change/100 << endl;
change %= 100;
cout << "25s= " << change/25 << endl;
change %= 25;
cout << "10s= " << change/10 << endl;
change %= 10;
cout << "5s= " << change/5 << endl;
change %= 5;
cout << "1s= " << change << endl;
cout << "END The amount you entered is: " << amount << endl;
}
return 0;
}
int amount;
cout << "Enter amount:" << endl;
while(!(cin >> amount)) {
string garbage;
cin.clear();
getline(cin,garbage);
cout << "Invalid amount. "
<< "Enter Numeric value for amount:" << endl;
}
I think you task relates to the so called defensive programming, one of it`s ideas is to prevent situations like one you described (function expects one type and user enters another).
I offer you to judge whether input is correct using method that returns stream state , which is good(),
so I think it will look something like this:
int amount = 0;
while (cin.good()) {
cout << "Enter amount:" << endl;
cin >> amount;
I am still working on a database for movies and I would like to show the user what he has input to the file.
However when i use cout << lisafilm << it provides me with hex value. Therefore, I need to conver hex to string.
Snippet of trouble.
void sisend()
{
string nimi;
int aasta;
long int hinne;
string vaadatud;
ofstream lisafilm("andmebaas.txt", ios::app);
cout <<"Sisestage filmi nimi." << endl;
cin >> nimi;
cout << "Sisestage filmi aasta." << endl;
cin >> aasta;
cout << "Sisestage filmi hinne." << endl;
cin >> hinne;
cout << "Kas olete filmi juba vaadanud?" << endl;
cout << "Vastake 'Jah' voi 'Ei'" << endl;
cin >> vaadatud;
lisafilm<< nimi << " " << aasta << " " << hinne<< " " << vaadatud << endl;
lisafilm.close();
{
system("CLS");
int hex_str = lisafilm ;
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
main();
}
when i use cout << lisafilm it provides me with hex value
This is because you are trying to output an ofstream. When this happens, operator void* gets called, producing an arbitrary hex sequence which is tied to your stream, but ultimately is very much useless.
Try this:
std::stringstream ss;
ss << std::hex << lisafilm;
const std::string s = ss.str();
lisafilm is a stream, not a string
If you want to copy lisafilm to cout something like cout << lisafilm.rdbuf(); would do the trick (assuming that lisafilm is an ostream or istream and that lisafilm's position is the start of the file.
Your code is very poorly formatted, I don't think what you posted would compile. If you clean it up stackoverflow may be able to help you more.