C++ cin.get before a loop
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
solution1.cpp:19: error: no matching function for call to‘int main()’
:‘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<_Cha开发者_运维百科rT, _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)
to x = cin.get()
, but from the context I'm guessing you want to read a whole number. For that, use cin >> x
.
If you're trying to read an integer from the command line, then the syntax is cin >> x;
. cin.get()
reads a single character, and returns it by value.
Use "x= cin.get();" instead of "cin.get(x);"
For more information see all variations of cin.get at below mentioned link,
http://www.cplusplus.com/reference/iostream/istream/get/
精彩评论