i am trying to make the user pick a number thats leads to a another code in c++ how do i do that?
cout << "Choose a text: "
getline(????)
1:
code number one
2.
text number 2
Pick a number?
I would assume you are talking about inputing a integer.
getline mostly is used for a string and spaces between each words, or a word.
For example if the input is like "Happy thanks giving"
the code would be like:
#include <iostream>
#include <string> //to use getline(cin, variable_name)
using namespace std;
int main()
{
string sentenceWithSpace;
cout << "get input" << endl;
getline(cin,sentenceWithSpace);
cout << sentenceWithSpace << endl;
system("pause"); // include this line if you use VS
return 0;
}
if the user is just inputing a value like 1,2,3,4,5,6
#include <iostream>
using namespace std;
int main()
{
int thisIsJustAInteger;
cout << "get input" << endl;
cin >> thisIsJustAInteger;
cout << thisIsJustAInteger << endl;
system("pause"); // include this line if you use VS
return 0;
}
Related
Basically if I write 15.20 I want 15.20 to return and if I write without any extra zeros or any rounding so that the 0 is still printed out.
I tried using setprecision, but it just adds a bunch of zeros.
my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double flyttal;
cout << "Skriv in ett flyttal: ";
cin >> flyttal;
cout << "Du skrev in flyttalet: " << fixed << setprecision(2) << flyttal << endl;
return 0;
}
Now thats fine for the float value 15.20 but if I want to write 1.315456 I will only get 1.32. I want to be able to get the same amount of decimals as I put in.
I am confused with the C++ function std::get_money defined in the <iomanip> header file. What is the use of get_money as per programming concept?
I have the following code using std::get_money.
#include <iostream> // std::cin, std::cout
#include <iomanip> // std::get_money
int main ()
{
long double price;
std::cout << "Please, enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) std::cout << "Error reading price\n";
else std::cout << "The price entered is: " << price << '\n';
return 0;
}
When I typed in an input of 100.25 it returned 100. What is the relation between the output and monetary format? I read this reference but cannot understand the relation. The same confusion is present with std::put_money, std::get_time, and std::put_time.
What are some examples of its actual use?
This is a part of the standard library that I didn't know existed! According to cppreference, you have to set the locale to define how time and money should be formatted. Here I'm using the en_US locale.
#include <iomanip>
#include <iostream>
int main() {
long double price;
std::cin.imbue(std::locale("en_US.UTF-8"));
std::cout << "Please enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) {
std::cout << "Error reading price\n";
} else {
std::cout << "The price entered is: " << price << '\n';
}
}
Still, this seems a bit finicky to me. The number must include a . with at least two digits after it. The $ is optional.
For my program, I have a function that gets the users input. I made the code so far, but how how can I make it where when I'm typing and if I go over the text limit, it will delete the previous character and not allow me to type anymore unless I backspace the characters.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Please enter your name: ";
char word[10];
cin >> setw(5) >> word;
cout << endl;
cout << "Your name is: " << word << endl;
system("pause");
return 0;
}
One way to do this is to use a library such as the GNU Readline Library. This library takes care of all the details involved in reading lines of text from the user, with line editing and features such as maximum length.
When I run this program and input, for example, the number 7, the final cout command only works occasionally. Otherwise, the program exits successfully but the result is not printed. Why is this happening?
#include <iostream>
#include <cmath>
double treble(double);
int main()
{
using namespace std;
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
return 0;
}
double treble(double n)
{
return n * 3;
}
You should put using namespace std; outside of all function declarations, right under your #include directives. Also, when you say it's not printing, is it that the console is closing before displaying your result? In that case, I would advocate using a simple cin to "pause" the program. You can do it exactly as #Nihar says, though I might suggest using a string instead of an int so that it doesn't break if you accidentally type something other than an int.
Something like this:
#include <iostream>
#include <cmath>
using namespace std;
double treble(double);
int main(){
cout << "Enter a number:" << endl;
double numways;
cin >> numways;
numways = treble(numways);
cout << "Your number trebled is: " << numways << endl;
string foo;
cin >> foo;
return 0;
}
double treble(double n){
return n * 3;
}
try with this => put
int temp;
cin>>temp;
before return 0; to pause the program, because the execution finished (successfully) before the last output could be written to the console.
When I run the following code and insert a new line (press enter) when prompted for
the golf structure, the second call to the function doesn't request input and finishes as if I've pressed enter again.
I've read up on : cin.get() , cin.clear() , cin.ignore(...) but nothing seems to help.
I'm pretty sure that it has nothing to do with multiple .cpp files and the header but I'm putting the code as is.
I'm using Visual Studio C++ 2010 - Express.
Thanks in advance for your help!
header file : golf.h
#ifndef GOLF_H
#define GOLF_H
const int Len = 40;
struct golf{
char fullname[Len];
int handicap;
};
int setgolf(golf & g );
void showgolf(const golf & g );
#endif
golf.cpp
#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
int setgolf(golf & g ){
cout << "Enter a name for the golf structure:" << endl;
if (cin.get(g.fullname,Len)) {
cin.get(); // deals with the '\n' incase the user inputs a valid string
return 1;
}
else{
return 0;
}
}
void showgolf(const golf & g ){
cout << "this golf structure contains the following information:" << endl;
cout << "name: " << g.fullname << endl ;
cout << "handicap: " << g.handicap << endl ;
}
main ()
#include "stdafx.h"
#include "golf.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
golf myGolf;
// check of int setgolf(golf & g );
int answ = setgolf(myGolf); //try to input empty string
showgolf(myGolf);
cout << "the number returned :" << answ << endl ;
answ = setgolf(myGolf); // normal string
showgolf(myGolf);
cout << "the number returned :" << answ << endl ;
return 0;
}
This problem happens when you just press enter in the first prompt. The input stream is marked as eof, an error condition flag (that's why it returns 0). The input stream then stops working.
It seems that you're using a kind of old C++, pre ISO 1998, while I don't think you need that. However, if you want to stick with your approach, then do the following: after cin.getline() (no need to return anything) write: cin.clear(); cin.sync();, as follows:
void setGolf(Golf &g)
{
cout << "Enter a name for the golf structure:" << endl;
getline( cin, g.fullname ) );
cin.clear();
cin.sync();
}
Now, about modernizing your code. First of all, you can use the standard library's class string, which is able to store a string literal, even growing if needed, without giving a maximum value of chars. This is somewhat confusing, since you are including the header string, which will include that class, but you're not using it. The use of string also comes with other advantages, such as automatically correcting the potential buffer overflow which could happen in your Golf structure. So I would change your structure to be:
struct Golf{
string fullname;
int handicap;
};
Now you can use getline(), in utility, which reads a whole line and stores it in string, doing all the magic for you. So you could change your golf.cpp file, to:
#include <utility>
//...
void setGolf(Golf &g)
{
cout << "Enter a name for the golf structure:" << endl;
getline( cin, g.fullname ) );
}
You can now also change the return type to void. It is not probable to experience an error of any kind while using getline(). Anyway, take into account that you could return bool (boolean type), which is a built-in type, with literals true and false.
I am certain that you could change your main() now, to a simpler style:
int main()
{
Golf myGolf;
setGolf(myGolf);
showGolf(myGolf);
setGolf(myGolf);
showGolf(myGolf);
return 0;
}
Finally, you could consider encapsulating your information in a class, instead of a struct, but that is a whole different issue.
Hope this helps.
You can also leave char[] instead of replacing it with string(I am still learning, so if I'm wrong please correct me). I think that when
std::cin.get(char *,Size) is not able to load characters, it's turning 2 bits on 0, fail and error, this is my solution:
std::cin.get(g.fullname, Len);
if(!std::cin)
{
std::cin.clear();
std::cin.get();
std::cout << "You inserted empty line." << std::endl;
return 0;
}