I'm new to the getline function in C .
I'm writing a simple program that creates a struct for a Fraction, which reads in a numerator and denominator. Then, I'm writing a void printFraction()
function which takes in the struct as a parameter and outputs the numerator divided by the denominator.
However, I'm getting an error that says that there is no matching function for getline
, and that it requires arguments but is only getting 2. How can I find what the problem is?
#include <iostream>
using namespace std;
struct Fraction {
int numerator;
int denominator;
};
void printFraction(Fraction f);
int main() {
Fraction f;
cout << "Please enter numerator";
getline(cin, f.numerator);
cout << "Please enter denominator";
getline(cin, f.denominator);
cin.ignore();
printFraction(f);
}
void printFraction(Fraction f) {
cout << f.numerator << "/" << f.denominator;
}
CodePudding user response:
First thing, you must include string
:
#include <string>
Also, getline
doesn't work with int
. So you'll have to take in a std::string
and then convert that string
into int
.
Consider not using the following in your code:
using namespace std;
..as it's considered as bad practice. For more info on why, look up to why is "using namespace std" considered as a bad practice.
So after all these fixes, here's the final code:
Final Code:
#include <iostream>
#include <string>
struct Fraction {
int numerator;
int denominator;
};
void printFraction(Fraction f);
int main() {
Fraction f;
std::string numerator, denominator;
std::cout << "Please enter numerator: ";
std::getline(std::cin, numerator);
std::cout << "Please enter denominator: ";
std::getline(std::cin, denominator);
f.numerator = std::stoi(numerator);
f.denominator = std::stoi(denominator);
printFraction(f);
}
void printFraction(Fraction f) {
std::cout << f.numerator << '/' << f.denominator;
}
CodePudding user response:
So, you can't use getline
on an integer int
, it reads the entire line as a string. Therefore you can either use std::cin for input or convert your line into an integer by doing the following
#include <iostream>
#include <string>
using namespace std;
struct Fraction {
int numerator;
int denominator;
};
void printFraction(Fraction f);
int main() {
Fraction f;
string line;
cout << "Please enter numerator";
getline(cin, line);
f.numerator = stoi(line); // stoi() converts a string to an integer
cout << "Please enter denominator";
getline(cin, line);
f.denominator = stoi(line);
cin.ignore();
printFraction(f);
}
void printFraction(Fraction f) {
cout << f.numerator << "/" << f.denominator;
}
CodePudding user response:
Problem
First of all include the string library
#include <string>
secondly getline
is used to read inputs in string format hence the error and third don't using namespace std;
it is considered a bad programming practice.
Solution
After obtaining the user input in string format use stoi
to convert the variable to an integer that will enable you to perform arithmetic operations
I have modified your code down below
#include <iostream>
#include <string>
struct Fraction {
int numerator;
int denominator;
};
void printFraction(Fraction f);
int main() {
Fraction f;
std::string numerator, denominator;
std::cout << "Please enter numerator";
std::getline(cin, f.numerator);
std::cout << "Please enter denominator";
std::getline(cin, f.denominator);
f.numerator = std::stoi(numerator);
f.denominator = std::stoi(denominator);
printFraction(f);
}
void printFraction(Fraction f) {
std::cout << f.numerator << "/" << f.denominator;
}
References