-1

I just started learning C++ and I was trying to create a simple code with Visual Studio 2017.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int x;
std::cout >> "Enter Age";
std::cin >> x;

    if (x >= 18) {
        std::cout << "You are an adult." << endl;
    }
    else {
        std::cout << "You are a child." << endl;
    }

    return 0;
}

But I think I was getting a lot of errors such as iostream didn't exist because I didn't see it in the header file or iostream.h I tried creating a new header file and simply typing #include <iostream> but that didn't seem to work. Here are my errors.

 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<char,_Traits> &std::operator >>
 (std::basic_istream<char,_Traits> &,signed char &)': could not deduce 
 template argument for 'std::basic_istream<char,_Traits> &' from 
 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1068): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<char,_Traits> &std::operator >>
 (std::basic_istream<char,_Traits> &,signed char *)': could not deduce 
 template argument for 'std::basic_istream<char,_Traits> &' from 
 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1061): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
 (std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template 
 argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1036): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
 (std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template 
 argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(995): note: 
 see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2676: binary '>>': 'std::ostream' does not define this operator or a 
 conversion to a type acceptable to the predefined operator
 1>Done building project "ConsoleApplication3.vcxproj" -- FAILED.

Thanks for any help.

Talita
  • 805
  • 3
  • 11
  • 31
Msyed
  • 27
  • 2

2 Answers2

0

Check this: std::cout >> "Enter Age";. I suppose it to be std::cout << "Enter Age"; instead.

BTW, not related to the question, since you are writing std::cout and std::cin then it would be better if you keep the consistency with std::endl instead of endl.

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • thanks after that I have no errors when I compiled it but when I tried to run the file i got this error. 1>------ Build started: Project: ConsoleApplication3, Configuration: Debug Win32 ------ 1>Source.obj : error LNK2005: _main already defined in ConsoleApplication3.obj 1>c:\users\munta\onedrive\documents\visual studio 2017\Projects\ConsoleApplication3\Debug\ConsoleApplication3.exe : fatal error LNK1169: one or more multiply defined symbols found 1>Done building project "ConsoleApplication3.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== – Msyed Jul 13 '17 at 01:44
  • @Msyed Check [this](https://stackoverflow.com/questions/26583763/error-lnk2005-main-already-defined-in-hold-obj) first and make sure you have only ONE main function. – duong_dajgja Jul 13 '17 at 01:48
  • Yes you are right. I had another main function in another file. thanks. – Msyed Jul 13 '17 at 01:53
0

Problem is in line 8. It shoud be:

std::cout << "Enter Age";
Talita
  • 805
  • 3
  • 11
  • 31