I just started learning C++, and this test seemed like a good idea so i tried doing it, doesn't seem to work, and it really doesn't make sense why (to me).
#include <iostream>
using namespace std;
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.32543; // Floating point number (with decimals)
char myLetter = 'H'; // Character
string myText = "test text: test"; // String (text)
bool myBoolean = true; // Boolean (true or false)
int main() {
cout << myNum << endl;
cin >> myNum >> endl;
cout << myFloatNum << endl;
cin >> myFloatNum >> endl;
cout << myLetter << endl;
cin >> myLetter >> endl;
cout << myText << endl;
cin >> myText >> endl;
cout << myBoolean << endl;
cin >> myBoolean >> endl;
return 0;
}
You forgot to include <string>, string isn't a basic C++ datatype; use #include <string> after iostream, without the spaces after the greater than and less than signs.
It does not make sense to cin something into endl. cin is a stream to get data from, but the endl is a thing to end the line, as #arsdever commented.
Simply remove it, and your code will compile:
#include <iostream>
#include <string> // You forgot to include that header, for using std::string
using namespace std;
int myNum = 5;
double myFloatNum = 5.32543;
char myLetter = 'H';
string myText = "test text: test";
bool myBoolean = true;
int main() {
cout << myNum << endl;
cin >> myNum;
cout << myFloatNum << endl;
cin >> myFloatNum;
cout << myLetter << endl;
cin >> myLetter;
cout << myText << endl;
cin >> myText;
cout << myBoolean << endl;
cin >> myBoolean;
return 0;
}
Although, you may want to first read the user's input, and then print it. Now, you print the predefined by you value of the variable (and then print an end of line), and then read the input from the user for that specific variable.
Related
How can I find the positions of repeated characters in a string? For example, the inputted string is "11-28-1995". My goal is to assign each of those values between the "-", a variable, so I would have month = 11, day - 28, and year = 1995. This is what I have got so far, although I am not sure how to iterate after the month.
#include <iostream>
using namespace std;
int main() {
string input;
cout << "What is the date?" << endl;
cin >> input;
int pos = input.find("-");
int month = stoi(input.substr(0, pos));
cout << month << endl;
cout << day << endl;
return 0;
}
Maybe you are looking for something like this:
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
int main() {
string input;
cout << "What is the date?" << endl;
cin >> input;
unsigned int day = 0;
unsigned int month = 0;
unsigned int year = 0;
stringstream ostr(input.c_str());
char c1, c2 = 0;
ostr >> day >> c1 >> month >> c2 >> year;
assert((c1 == c2) && (c2 == '/'));
cout << "Day: " << day << endl;
cout << "Month: " << month << endl;
cout << "Year: " << year << endl;
return 0;
}
For a more general token extractor, you may be interested in the use of getline:
use getline and while loop to split a string
Hope it helps ;)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//void FillNames(vector<string> & vecNames);
//void SortNames(vector<string> & vecNames);
int main() {
string firstName;
int i = 0;
cout << "Information:" << endl;
cout << "EOF character in windows is Control + Z" << endl;
cout << "and EOF character on Mac is Control + D:" << endl;
cout << "-----------------------------------------------" << endl;
while (i < 13) {
cout << "Enter first name only in all caps (example: JOHN)" << endl;
cout << "Enter EOF character to exit name entry: ";
cin >> firstName;
i++;
}
}
Here is a link to what i'm trying to accomplish.
https://drive.google.com/file/d/1AuW9hQPbd1f294dCZN6Q8Ac6lgIy0Ivf/view?usp=sharing
You can use the stream's input state to break the loop when EOF is entered:
while (i < 13) {
cout << "Enter first name only in all caps (example: JOHN)" << endl;
cout << "Enter EOF character to exit name entry: ";
if (cin >> firstName)
i++;
else
break;
}
First off I'd like to say I know that using getline is a better alternative, however, i am curious as to why this code doesn't work as intended: and I can't figure out why
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
if(cin.fail()) {
char rd = cin.get();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
}
}
return 0;
}
Intended: if an integer is entered, continue otherwise cin fails, we pull one char from stdin and essentially output it. Then we keep looping.
The way I see it, eventually cin.get() should clear up bad input; but it never does: it gets stuck in an infintie loop. What?
cin.fail() detects whether the value entered fits the value defined in the variable.
But if cin.fail() is true, it means that
a) the entered value does not fit the variable
b) the varialbe will not be affected
c) the instream is still broken
d) the entered value is still in the buffer and will be used for the next "cin >> variable"statement.
Hence you have to do the following:
a) repair the instream via cin.clear();
b) clear the buffer with cin.ignore(std::numeric_limits::max(),'\n');
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
if(cin.fail()) {
char rd = cin.get();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
cin.clear();
cin.ignore();
}
}
return 0;
}
After taking the input integer, it gets a newline as a character when you press enter. Same as for character input
#include <iostream>
#include <string>
using namespace std;
int main() {
while(1) {
int input;
cout << "---> ";
cin >> input;
getchar();
if(cin.fail()) {
char rd = cin.get();
getchar();
cout << "failure" << rd << "=" << cin.fail() << " " << endl;
}
}
return 0;
}
In case of an invalid input, I want extract the invalid input from cin and store it in a variable that fits. How can I do that?
#include<iostream>
using namespace std;
int main(){
int number;
cout << "Enter a number: " << endl;
cin >> number;
if(cin.fail()){
cout << "Error!" << endl;
//HOW CAN I STORE THE INVALID INPUT WHICH IS STORED IN cin INTO A STRING?
}
return 0;
}
When you detect that failbit is set, reset it, and use std::getline to read entire line of invalid input into std::string from std::cin:
#include <iostream>
#include <string>
int main()
{
int number;
while(true)
{
std::cout << "Enter a number (0 to exit): " << std::endl;
std::cin >> number;
if(std::cin.fail())
{
std::string error_data;
std::cin.clear(std::cin.rdstate() & ~std::ios::failbit);
std::getline(std::cin, error_data);
std::cout << "You didn't enter a number - you've entered: " << error_data << std::endl;
}
else
{
std::cout << "Number is: " << number << std::endl;
if(number == 0)
{
break;
}
}
}
return 0;
}
I made a program using structure and pointer. But for some reason it is not working properly. The main problem is that, for-loop would no go as it would. It would be helpful if you could solve this problem
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Book{
string name;
int release;
};
int main(){
//local variable
int i;
string release_dte;
int choice;
//interface
cout << "Welcome to Book Storage CPP" << endl;
cout << "How many entries would you like to make: ";
cin >> choice;
Book* Issue = new Book[choice];
//for handler
for (i = 0; i < choice; i++){
cout << "Book: ";
getline(cin, Issue[i].name);
cout << "Release Date: ";
getline(cin, release_dte);
Issue[i].release = atoi(release_dte.c_str());
}
cout << "These are your books" << endl;
for ( i = 0; i < choice; i++){
cout << "Book: " << Issue[i].name << " Release Date: " << Issue[i].release << endl;
}
system("pause");
return 0;
}
You're not checking if the input succeeded, nor are you clearing the new line left after the extraction into choice:
if ((std::cout << "Book: "),
std::getline(std::cin >> std::ws, Input[i].name) &&
(std::cout << "Release Date: "),
std::getline(std::cin >> std::ws, release_dte))
{
Input[i].release = std::stoi(release_dte);
}
You should also be using std::stoi for C++ strings as shown above.
I could not exactly infer what is the problem you are referring to. But my guess is getline() function inside the for loop is not working properly, I suggest the code before the for loop to be like the following\
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
for (i = 0; i < choice; i++){
cout << "Book: ";
getline(cin, Issue[i].name);
cout << "Release Date: ";
getline(cin, release_dte);
Issue[i].release = atoi(release_dte.c_str());
}
Your final code should be
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Book{
string name;
int release;
};
int main(){
//local variable
int i;
string release_dte;
int choice;
//interface
cout << "Welcome to Book Storage CPP" << endl;
cout << "How many entries would you like to make: ";
cin >> choice;
Book* Issue = new Book[choice];
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
//for handler
for (i = 0; i < choice; i++){
cout << "Book: ";
getline(cin, Issue[i].name);
cout << "Release Date: ";
getline(cin, release_dte);
Issue[i].release = atoi(release_dte.c_str());
}
cout << "These are your books" << endl;
for ( i = 0; i < choice; i++){
cout << "Book: " << Issue[i].name << " Release Date: " << Issue[i].release << endl;
}
system("pause");
return 0;
}
This will work as you intended