little help for me please

Posts 1–15 of 19 · Page 1 of 2
little help for me please
I get 2 errors when I compile this:

syntax error: identificator "age"
instruction else without if

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

int main()
{
int name;
int age;

cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Fine, how old are you?" << endl;
cin >> age;
if age < 18 or == 18; {
cout << "Hi" << name << ", welcome to my application!"<< endl;
}
else
cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
return 0;
}
[php]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int name;
int age;

cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Fine, how old are you?" << endl;
cin >> age;
if (age < 18 || age == 18) {
cout << "Hi" << name << ", welcome to my application!"<< endl;
} else {
cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
}
return 0;
}
[/php]

Fixed.

If/else syntax:
[php]
if( //condition )
{
//
} else {

}
[/php]

if/elseif:
[php]
if( //condition )
{
//
} else if( //condition )
{
//
}
[/php]
Quote Originally Posted by Void View Post
[php]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int name;
int age;

cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Fine, how old are you?" << endl;
cin >> age;
if (age < 18 || age == 18) {
cout << "Hi" << name << ", welcome to my application!"<< endl;
} else {
cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
}
return 0;
}
[/php]

Fixed.
Thank you

edit: When i execute the app, after i say my name it says idk what and it close
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
        int name;
        int age;

        cout << "Hi, what is your name?" << endl;
        cin >> name;
        cout << "Fine, how old are you?" << endl;
        cin >> age;
        if (age >= 18) {
        cout << "Hi" << name << ", welcome to my application!"<< endl;
        } else {
                cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
        }
cin.get();
        return 0;
}
fixed again
Use cin.get() to wait for input. Toxic put it in there for you already.
both doesnt work lol. It doens't let me enter my age and auto-attribute me one.
[php]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
string name;
int age;

cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Fine, how old are you?" << endl;
cin >> age;
if (age >= 18) {
cout << "Hi" << name << ", welcome to my application!"<< endl;
} else {
cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
}
cin.get();
return 0;
}
[/php]

Fixed once more, just realized you declared 'name' as an integer... That means your name can only be numbers. |:
Quote Originally Posted by Void View Post
[php]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
string name;
int age;

cout << "Hi, what is your name?" << endl;
cin >> name;
cout << "Fine, how old are you?" << endl;
cin >> age;
if (age >= 18) {
cout << "Hi" << name << ", welcome to my application!"<< endl;
} else {
cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
}
cin.get();
return 0;
}
[/php]

Fixed once more, just realized you declared 'name' as an integer... That means your name can only be numbers. |:

In order to use the string variable, don't you need to include string.h?
Code:
#include "stdafx.h"
#include <iostream>
#include "string.h"
using namespace std;

int main()
{
        string name;
        int age;

        cout << "Hi, what is your name?" << endl;
        cin >> name;
        cout << "Fine, how old are you?" << endl;
        cin >> age;
        if (age >= 18) {
        cout << "Hi" << name << ", welcome to my application!"<< endl;
        } else {
                cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
        }
        cin.get();
        return 0;
}
I am getting 3 errors, all saying binary: no operartor found that accept command for std::string

EDIT: I found out how to fix it. It is not a string, but a char.
Code:
int age;
char name;
EDIT2:And btw, it does not let me enter my age it close right after i say my name.

Hey guys, I solve my problem by trying and getting error and repairing it. Here is the final code and it works 100% ^^

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

int main()
{
        char name[] = "pseudo";
        int age = 15;

        cout << "Hi, what is your name?" << endl;
        cin >> name;
		cout << "Fine, how old are you?" << endl;
        cin >> age;
        if (age >= 18) {
        cout << "Hi" << name << ", welcome to my application!"<< endl;
        } else {
                cout << "Hi " << name <<", sorry but you are not old enought for my application." << endl;
        }
        cin.get();
		system("pause");
        return 0;
}
thx for help
Quote Originally Posted by naghal707 View Post
Code:
#include "stdafx.h"
#include <iostream>
#include "string.h"
using namespace std;

int main()
{
        string name;
        int age;

        cout << "Hi, what is your name?" << endl;
        cin >> name;
        cout << "Fine, how old are you?" << endl;
        cin >> age;
        if (age >= 18) {
        cout << "Hi" << name << ", welcome to my application!"<< endl;
        } else {
                cout << "Hi" << name <<", sorry but you are not old enought for my application." << endl;
        }
        cin.get();
        return 0;
}
I am getting 3 errors, all saying binary: no operartor found that accept command for std::string

EDIT: I found out how to fix it. It is not a string, but a char.
Code:
int age;
char name;
EDIT2:And btw, it does not let me enter my age it close right after i say my name.

Hey guys, I solve my problem by trying and getting error and repairing it. Here is the final code and it works 100% ^^

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

int main()
{
        char name[] = "pseudo";
        int age = 15;

        cout << "Hi, what is your name?" << endl;
        cin >> name;
		cout << "Fine, how old are you?" << endl;
        cin >> age;
        if (age >= 18) {
        cout << "Hi" << name << ", welcome to my application!"<< endl;
        } else {
                cout << "Hi " << name <<", sorry but you are not old enought for my application." << endl;
        }
        cin.get();
		system("pause");
        return 0;
}
thx for help
[php]
#include <iostream>
#include <string> // including the string.h file
#include <sstream> // including the stringstream
using namespace std;

int main()
{
string ITSASTRING; // empty string, used for transfering variables
string firstname; // second string, used for first name
string lastname; // third string, used for last name
int age; // int, used for age

cout << "Hi, what is your FIRST name?" << endl;
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> firstname; // transferring data from ITSASTRING to firstname
cout << "Alright " << firstname << " what is your last?";
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> lastname; // transferring data from ITSASTRING to lastname

cout << "alrighty then " << firstname<<" " <<lastname <<", how old are you?" << endl; // displaying firstname lastname and asking how old
cin >> age; // the usual cin >> age
if (age >= 18) { // if age is equal to or less then 18 do the following
cout << "Hi " << firstname<< " " <<lastname << ", welcome to my application!"<< endl;
}
else { // age was more then 18 so we do the following
cout << "Hi " << firstname<<" " <<lastname <<", sorry but you are not old enough for my application." << endl;
}
cin.get(); // waiting for input from user
return 0; // returning 0 to int main

// yes, I did explain a lot. But other people may read this and not understand it, so I felt it was important.
}[/php]
What I did, was use strings instead of chars, chars are 1 byte... so for a 4 letter word you need 4 chars. Make sense? Strings are kinda like multiple chars... Kinda.

By just assigning the input to the strings using cin >> firstname it would skip all the questions, so I used this method and explained it using comments. Hope I helped you

This works perfectly for me, and it should work for you. If you are required to add in that stdfax or w.e then go ahead and include it but I don't need to...
[php]#include <iostream>
#include <string>

using namespace std;

int main()
{
int age;
string name;
cout << "Hi. What's your name?\n";
cin >> name;
cout << "And what's your age?\n";
cin >> age;
if(age >= 18)
{
cout << "Welcome to my program, " << name << '\n';
}
else
{
cout << "Sorry " << name << ", you're too young\n";
}

cin.get(); //Not needed for Microsoft Visual C++
return 0;
}[/php]
Works 100% on Visual C++ 2008
[php]
#include <iostream>
#include <string> // including the string.h file
#include <sstream> // including the stringstream
using namespace std;

int main()
{
string ITSASTRING; // empty string, used for transfering variables
string firstname; // second string, used for first name
string lastname; // third string, used for last name
int age; // int, used for age

cout << "Hi, what is your FIRST name?" << endl;
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> firstname; // transferring data from ITSASTRING to firstname
cout << "Alright " << firstname << " what is your last?";
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> lastname; // transferring data from ITSASTRING to lastname

cout << "alrighty then " << firstname<<" " <<lastname <<", how old are you?" << endl; // displaying firstname lastname and asking how old
cin >> age; // the usual cin >> age
if (age >= 18) { // if age is equal to or less then 18 do the following
cout << "Hi " << firstname<< " " <<lastname << ", welcome to my application!"<< endl;
}
else { // age was more then 18 so we do the following
cout << "Hi " << firstname<<" " <<lastname <<", sorry but you are not old enough for my application." << endl;
}
cin.get(); // waiting for input from user
return 0; // returning 0 to int main

// yes, I did explain a lot. But other people may read this and not understand it, so I felt it was important.
}[/php]
[php]
#include <iostream>
#include <string> // including the string.h file
#include <sstream> // including the stringstream
using namespace std;

int main()
{
string ITSASTRING; // empty string, used for transfering variables
string firstname; // second string, used for first name
int age; // int, used for age

cout << "Hi, what is your FIRST name?" << endl;
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> firstname; // transferring data from ITSASTRING to firstname

cout << "alrighty then " << firstname<<" how old are you?" << endl; // displaying firstname and asking how old
cin >> age; // the usual cin >> age
if (age >= 18) { // if age is equal to or less then 18 do the following
cout << "Hi " << firstname<< ", welcome to my application!"<< endl;
}
else { // age was more then 18 so we do the following
cout << "Hi " << firstname<< ", sorry but you are not old enough for my application." << endl;
}
cin.get(); // waiting for input from user not required with microsoft visual c++
return 0; // returning 0 to int main

// yes, I did explain a lot. But other people may read this and not understand it, so I felt it was important.
}[/php]


1st code can be used for both names, second code can be used for just the first name.

Both of these work 100% for microsoft visual c++ 2010, my way teaches, korean's is simple. Although, for me, cin >> wasn't working properly anyways...
Quote Originally Posted by VvITylerIvV View Post
[php]
#include <iostream>
#include <string> // including the string.h file
#include <sstream> // including the stringstream
using namespace std;

int main()
{
string ITSASTRING; // empty string, used for transfering variables
string firstname; // second string, used for first name
string lastname; // third string, used for last name
int age; // int, used for age

cout << "Hi, what is your FIRST name?" << endl;
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> firstname; // transferring data from ITSASTRING to firstname
cout << "Alright " << firstname << " what is your last?";
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> lastname; // transferring data from ITSASTRING to lastname

cout << "alrighty then " << firstname<<" " <<lastname <<", how old are you?" << endl; // displaying firstname lastname and asking how old
cin >> age; // the usual cin >> age
if (age >= 18) { // if age is equal to or less then 18 do the following
cout << "Hi " << firstname<< " " <<lastname << ", welcome to my application!"<< endl;
}
else { // age was more then 18 so we do the following
cout << "Hi " << firstname<<" " <<lastname <<", sorry but you are not old enough for my application." << endl;
}
cin.get(); // waiting for input from user
return 0; // returning 0 to int main

// yes, I did explain a lot. But other people may read this and not understand it, so I felt it was important.
}[/php]
[php]
#include <iostream>
#include <string> // including the string.h file
#include <sstream> // including the stringstream
using namespace std;

int main()
{
string ITSASTRING; // empty string, used for transfering variables
string firstname; // second string, used for first name
int age; // int, used for age

cout << "Hi, what is your FIRST name?" << endl;
getline (cin,ITSASTRING); // getting the input from this line and inputting it into ITSASTRING
stringstream(ITSASTRING) >> firstname; // transferring data from ITSASTRING to firstname

cout << "alrighty then " << firstname<<" how old are you?" << endl; // displaying firstname and asking how old
cin >> age; // the usual cin >> age
if (age >= 18) { // if age is equal to or less then 18 do the following
cout << "Hi " << firstname<< ", welcome to my application!"<< endl;
}
else { // age was more then 18 so we do the following
cout << "Hi " << firstname<< ", sorry but you are not old enough for my application." << endl;
}
cin.get(); // waiting for input from user not required with microsoft visual c++
return 0; // returning 0 to int main

// yes, I did explain a lot. But other people may read this and not understand it, so I felt it was important.
}[/php]


1st code can be used for both names, second code can be used for just the first name.

Both of these work 100% for microsoft visual c++ 2010, my way teaches, korean's is simple. Although, for me, cin >> wasn't working properly anyways...
I prefer my code, much more simple and clearer.
Quote Originally Posted by naghal707 View Post
I prefer my code, much more simple and clearer.
lol yeah, I didn't really organize this much... just threw it together. Throw in a few empty lines and mine looks really neat, especially with the comments deleted. But the cin >> method wasn't working properly for me, figured others might also be having a problem so I used my method.


When I was testing your method just now, I noticed that when the user enters two words for the first question, it skips all the other questions and goes to cin.get();

My method does not have this problem
Neither does mine
Posts 1–15 of 19 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?