cin/cout Undeclared

Pages: 12
I'll admit I don't have TOO much experience with coding, but I've never seen this error before. It tells me that cin, cout, and endl are all undeclared and I have no idea how to declare them. I'm using Bloodshed if that helps any. Helping with any other things I may have gotten wrong would be very appreciated as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include stdafx;
#include <iostream>

int FavFood;
int Name;
int Age;

int main()
{
    cout << "Hello, I am Chat-Bot. What is your name?" >> endl; //Ask for name
    cin >> Name >> endl; //recieve variable Name
    cout >> "Nice to meet you, " >> Name >> ". How old are you?" >> endl; //Reply using given name and ask for age
    cin >> Age >> endl; // Recieve age
    cout >> "Ah, so you're " >> Age >> " years old. What is your favorite food?" >> endl; //repeat age and ask for variable FavFood
    cin >> FavFood >> endl; //recieve FavFood
    cout << "Mine is spaghetti. Well, it was nice talking to you!" << endl; //Reply and end conversation
    cout << "Quick Recap:" << endl; //Recap all variables given below
    cout << "Name = " << Name << endl; //variable Name
    cout << "Age = " << Age << endl; //variable Age
    cout << "Favorite Food = " << FavFood << endl; //variable FavFood
}


By the way, the length (without this bit) is 1234. I thought that was neat.
Last edited on
Whatever you're learning c++ from is out of date. Things in the standard libraries are now in the namespace std. You need to prefix cout, cin etc with std::.
Thanks for the quick reply, but does this apply to endl; as well? I seem to be getting an error with that too. also, this isn't quite up to date, I changed it just a little after posting. Just a minor chage:
 
#include "stdafx.h" 


EDIT: Actually, let me just paste the contents of my Compiler tab:
1 C:\Dev-Cpp\Untitled2.cpp stdafx.h: No such file or directory.
C:\Dev-Cpp\Untitled2.cpp In function `int main()':
10 C:\Dev-Cpp\Untitled2.cpp `endl' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
12 C:\Dev-Cpp\Untitled2.cpp no match for 'operator>>' in 'std::cout >> "Nice to meet you, "'
14 C:\Dev-Cpp\Untitled2.cpp no match for 'operator>>' in 'std::cout >> "Ah, so you\'re "'
C:\Dev-Cpp\Makefile.win [Build Error] [Untitled2.o] Error 1
Last edited on
does this apply to endl; as well?
Yes, as endl is in the std namespace. http://www.cplusplus.com/doc/tutorial/namespaces/
This is cause you do not have using name space std; this makes the IDE automatically include std:: like cout cin endl and anything stored within the classes of std library. So you might be wondering why would we use std:: if we could just include using namespace std. That is because if you use other libraries there is a chance functions could conflict between them (if there are similar names ex. std::example1 and anotherclass::example1. So for now it's ok to use namespace std but when you start using other libraries you should start just typing std::
Last edited on
Okay, so after getting that put in, I get this as a result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>

int FavFood;
int Name;
int Age;

int main();
using namespace std;
{
    std::cout << "Hello, I am Chat-Bot. What is your name?" >> std::endl;
    std::cin >> Name >> std::endl;
    std::cout >> "Nice to meet you, " >> Name >> ". How old are you?" >> std::endl;
    std::cin >> Age >> std::endl;
    std::cout >> "Ah, so you're " >> Age >> " years old. What is your favorite food?" >> std::endl;
    std::cin >> FavFood >> std::endl;
    std::cout << "Mine is spaghetti. Well, it was nice talking to you!" << std::endl;
    std::cout << "Quick Recap:" << std::endl;
    std::cout << "Name = " << Name << std::endl;
    std::cout << "Age = " << Age << std::endl;
    std::cout << "Favorite Food = " << FavFood << std::endl;
}

Would this be correct?
Last edited on
Looks good to me :)
Okay, I may need to stop using Bloodshed. I still have 3 errors after this:
1. stdafx.h doesn't exist
2. It expected an unqualified-id before the { on line 10
3. It also expected a , or ; in the same place.
Oh u need to put namespace before main so do it like this
1
2
3
4
using namespace std;

Int main()
{


On an ipad so didn't notice the using misplace XD
Last edited on
Sorry, but that did absolutely nothing. (Not meant to be read in a sarcastic tone.) Could we start with problem 1 first?
Hmm I'm still fairly new to c++ myself only 3 weeks in and I haven't had to use that header for my programs yet.

With problem 2 your are typing a meal out but what you are telling your program with int is that you would input a whole number value. What you want to use for multi character inputs is a string.
Such as string favFood.
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
Using namespace std;
int main()
{

string fullname;
cout << "Enter name: ";
Cin>>fullname;
cout << fullname;
 return 0;
}


And with the switch between main and namespace that should of solved number 3
Last edited on
I just removed stdafx.h, I was just told I needed to include that but evidently I don't, so that solves 1.
2 and 3 still remain, the above resolution solved nothing.
You need to use using namespace std;
There are a few mistakes in lines 10,11,12,13,14,15;
You cannot use cin>>(variable>>endl;
Also cout uses the insertion operator << and not the extraction operator >>.
As far as stdafx is concerned i do not know where it is. (Are you using CodeBlocks ?)
1. I am using that now, sorry.
2. How can I fix it so that it works correctly?
3. Okay, I'll fix it ASAP.
4. No, I'm using Bloodshed Dev C++ as mentioned in my post.
EDIT: My code now looks like this after wanna be a programmer's suggestions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main();
{
    string Name;
    string Age;
    string FavFood
    std::cout >> "Hello, I am Chat-Bot. What is your name?" >> std::endl;
    getline(cin, Name);
    std::cout >> "Nice to meet you, " >> Name >> ". How old are you?" >> std::endl;
    getline(cin, Age);
    std::cout >> "Ah, so you're " >> Age >> " years old. What is your favorite food?" >> std::endl;
    getline(cin, FavFood);
    std::cout >> "Mine is spaghetti. Well, it was nice talking to you!" << std::endl;
    std::cout >> "Quick Recap:" << std::endl;
    std::cout >> "Name = " << Name << std::endl;
    std::cout >> "Age = " << Age << std::endl;
    std::cout >> "Favorite Food = " << FavFood << std::endl;
}
Last edited on
Oh no, you did just the opposite as i told you.
Since you have already written using namespace std; no need to prefix std:: everywhere (remove it, that will clear up the code for reading)and use cout<< and NOT cout>>
ok i think i got it sorry really tired so making a bunch of stupid oversites myself remove the ; after main(). change name to a string as well and do the same command i showed you for your favorite food and put namespace before your string and int declarations at the top cause if u dont you would need to input std::string
Last edited on
This is how your fixed up code should look like.
Try it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// #include "stdafx";
#include <iostream>
using namespace std;
string FavFood;
string Name;
string Age;

int main()
{
    cout << "Hello, I am Chat-Bot. What is your name?" << endl; //Ask for name
    cin >> Name; //recieve variable Name
    cout << "Nice to meet you, " << Name << ". How old are you?" << endl; //Reply using given name and ask for age
    cin >> Age; // Recieve age
    cout << "Ah, so you're " << Age << " years old. What is your favorite food?" << endl; //repeat age and ask for variable FavFood
    cin >> FavFood; //recieve FavFood
    cout << "Mine is spaghetti. Well, it was nice talking to you!" << endl; //Reply and end conversation
    cout << "Quick Recap:" << endl; //Recap all variables given below
    cout << "Name = " << Name << endl; //variable Name
    cout << "Age = " << Age << endl; //variable Age
    cout << "Favorite Food = " << FavFood << endl; //variable FavFood
}
Besides the ; thing, I'm fairly sure that all of that has already been done, gobiking.
But do you mean like this, programmer?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
    string Name;
    string Age;
    string FavFood;
    cout << "Hello, I am Chat-Bot. What is your name?" >> std::endl;
    getline(cin, Name);
    cout << "Nice to meet you, " >> Name >> ". How old are you?" >> std::endl;
    getline(cin, Age);
    cout << "Ah, so you're " >> Age >> " years old. What is your favorite food?" >> std::endl;
    getline(cin, FavFood);
    cout << "Mine is spaghetti. Well, it was nice talking to you!" << std::endl;
    cout << "Quick Recap:" << std::endl;
    cout << "Name = " << Name << std::endl;
    cout << "Age = " << Age << std::endl;
    cout << "Favorite Food = " << FavFood << std::endl;
}
Last edited on
I have posted the entire code for you just copy-paste and try it
Oh, I must have missed it.



IT WORKS! Thanks programmer, I'll study this and figure out what I did wrong.
Pages: 12