hey guys, i’m designing a C++ program that reads a sequence of one or more positive real numbers terminated by a negative number. To test that this has been done correctly i’m outputting the 5th number entered by the user. i am using a while loop to populate my array HOWEVER, my cin.get(x) is making my program not compile. please help my fix my code.
P.S this is the compliers error(s) if it is any help:
solution1.cpp: In function
‘int main()’:
solution1.cpp:19: error: no matching function for call to‘std::basic_istream<char, std::char_traits<char> >::get(int&)’/usr/include/c++/4.4/istream:280:
note: candidates are:typename std::basic_istream<_CharT, _Traits>::int_type std::basic_istream<_CharT, _Traits>::get() [with _CharT = char, _Traits = std::char_traits<char>]/usr/include/c++/4.4/istream:294:
note:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT&) [with _CharT = char, _Traits = std::char_traits<char>]/usr/include/c++/4.4/istream:321:
note:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]/usr/include/c++/4.4/istream:332:
note:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]/usr/include/c++/4.4/istream:355:
note:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]/usr/include/c++/4.4/istream:365:
note:std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::get(std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]
#include <iostream>
using namespace std;
int main()
{
char num[100] = {0};
int y = 0;
int x = 0;
int flag = 0;
cout << "Please enter line of numbers: ";
while (flag > 0)
{
cin.get(x);
if (x < 0)
{
flag = -1;
}
else
{
num[y] = x;
y = y + 1;
}
}
cout << " " << num[4] << endl;
return 0;
}
If you are really trying to read character-by-character, change
cin.get(x)tox = cin.get(), but from the context I’m guessing you want to read a whole number. For that, usecin >> x.