FAQ: Variables - User Input

When I entered my tip, the program didn’t output anything back. It says that my code works but it doesn’t output anything.

1 Like

Hello, @randomstuff099309100, and welcome to the forums.

Did you click Run after adding the std::cout << line, and prior to compiling? The Run button saves your code. If you compile after changing your code in the editor, but don’t click Run first, you just re-compile your previously saved code.

2 Likes

It’s not a huge issue, but the spacing for the output is bugging me. Not sure why it’s doing this when I’ve copied the spacing and added some more. I’m not sure why this is happening.

1 Like

Hello, @course2661269394, and welcome to the forums.

Did you click Run after adding the spaces in your code, and then compile the code? If you don’t click Run, you are re-compiling the same code as before. Clicking Run saves your code, and is a necessary step prior to compiling if you want to see your changes in action.

1 Like

I completely missed that, it works correctly now. Thanks!

1 Like

It would be easier to remember if the button were labelled Save instead of Run, but sometimes we just have to deal with what is, and move on. :man_shrugging:
Happy coding!

Thank you midlindner. Just started c++ today and I was having the exact same problem. glad I saw your comment.

1 Like

Saw up ahead that I have to click “run”… OOPS

I do not think that you were supposed to put the $ sign. Int type variables cannot hold characters, only direct integers.

Hello everyone after switching things around for awhile I cannot figure out why the “100dollars.” sentence is being put together. Could someone please help me

I don’t know, same thing happened. junkadress4665322822, it automatically puts the dollar sign there, only the number is counted as part of the input.

Nevermind, just hit the run button before compiling. just found out from a different comment.

I got the same problem but when I changed from g++ tip.cpp -o tip and ./tip, it worked for me. I think for the code to work, try not using ./a.out by changing the name to something else, in my case I called it tip, so when I run my code, I typed in ./tip and not ./a.out and it worked. I hope this helps.

I got this error can someone explain

Hello, I also have a question reagarding the tip exercise. Why can´t it find the file when I compile “g++ tip.cpp”
as usual?

Hello, @net7430021250, and welcome to the forums.

Did you navigate to another directory in the bash console? You can type pwd and press <enter> to show the current directory. It should be:

/home/ccuser/workspace/cpp-variables-tip

From your screenshot, you’ve already completed the exercise, so you could simply move on. You could also copy the code, reset the exercise, paste the code back in, click Run, and then execute the command to compile your code.

1 Like

why we need to intialized tip with 0 first
int tip = 0;
why don’t we just write this
int tip;

So idk if this bothers any one else or if any of yall noticed this but when you type the code std::cout << “You paid " << tip << " dollars.\n”; there is a period. It bugs me that the compile panel doesn’t add the period when you type in the tip amount. I was wondering why it doesn’t appear and if possible how the period can be displayed.

Something is wrong with my bash terminal. The hint shows me:
The completed tip.cpp should look like:

#include <iostream> 
int main() {   
int tip = 0;   
std::cout << "Enter tip amount: ";  
std::cin >> tip;   
std::cout << "You paid " << tip << " dollars.\n";
 }

I run $ g++ tip.cpp and it gives me this:

tip.cpp: In function ‘int main()’:
tip.cpp:9:12: error: no match for ‘operator>>’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘const char [9]’)
std::cout>>“You paid”>>tip>>“dollars\n”

In file included from /usr/include/c++/7/iostream:40:0,
              from tip.cpp:1:
/usr/include/c++/7/istream:980:5: note: candidate: template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&)
  operator>>(_Istream&& __is, _Tp&& __x)
  ^~~~~~~~
/usr/include/c++/7/istream:980:5: note:   template argument deduction/substitution failed:
/usr/include/c++/7/istream: In substitution of ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_ostream<char>&; _Tp = const char (&)[9]]’:
tip.cpp:9:14:   required from here
/usr/include/c++/7/istream:980:5: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
/usr/include/c++/7/istream:808:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
  operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
  ^~~~~~~~
/usr/include/c++/7/istream:808:5: note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/iostream:40:0,
              from tip.cpp:1:
/usr/include/c++/7/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
  operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
  ^~~~~~~~
/usr/include/c++/7/istream:803:5: note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/iostream:40:0,
              from tip.cpp:1:
/usr/include/c++/7/istream:761:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
  operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
  ^~~~~~~~
/usr/include/c++/7/istream:761:5: note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/iostream:40:0,
              from tip.cpp:1:
/usr/include/c++/7/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
  operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
  ^~~~~~~~
/usr/include/c++/7/istream:756:5: note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/istream:991:0,
              from /usr/include/c++/7/iostream:40,
              from tip.cpp:1:
/usr/include/c++/7/bits/istream.tcc:931:5note: candidate: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
  operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
  ^~~~~~~~
/usr/include/c++/7/bits/istream.tcc:931:5note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/istream:991:0,
              from /usr/include/c++/7/iostream:40,
              from tip.cpp:1:
/usr/include/c++/7/bits/istream.tcc:963:5note: candidate: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
  operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
  ^~~~~~~~
/usr/include/c++/7/bits/istream.tcc:963:5note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n"
           ^~~~~~~~~~
In file included from /usr/include/c++/7/string:53:0,
              from /usr/include/c++/7/bits/locale_classes.h:40,
              from /usr/include/c++/7/bits/ios_base.h:41,
              from /usr/include/c++/7/ios:42,
              from /usr/include/c++/7/ostream:38,
              from /usr/include/c++/7/iostream:39,
              from tip.cpp:1:
/usr/include/c++/7/bits/basic_string.tcc:1465:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
  operator>>(basic_istream<_CharT, _Traits>& __in,
  ^~~~~~~~
/usr/include/c++/7/bits/basic_string.tcc:1465:5: note:   template argument deduction/substitution failed:
tip.cpp:9:14: note:   ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
std::cout>>"You paid">>tip>>"dollars\n”
$

but Codecademy still puts a check to all 3 tasks

Can somebody help me?
#include <iostream>

int main() 
{
  int tip = 0;
  
  std::cout << "Enter tip amount: ";
  std::cin >> tip;
  std::cout << tip >> std::endl;
}

When I enter the tip (for example 1) I get
bash $ 1 not found.
clearly cin is not properly configured to work with this console.
I know what I am doing with c++ well beyond the basics of iostream and standard namespace functions.
This is not a syntax issue. It is a poorly developed console issue.

I want to upgrade to pro. But not until issues like this are fixed.

I can pass the test no problem.
I want to see the output as it should be displayed.

2 Likes