C++ Address Book - c++

I am trying to make it so if userselection = 1, then the user is asked questions to create their contact for an address book. It saves all of the contact info to a struct and then saves to a .txt file. I am very new to C++. This is what I have so far... I keep getting [Error] expected primary-expression before '.' token. <---- HOW CAN I FIX THIS
Also, can anyone offer guidance for how to save the struct to a file?
Thanks.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main(){
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == '1');
person newPerson;
cout << "What is your Name?" << endl;
cin >> person.Name;
cout << "What is your Address?" << endl;
cin >> person.Address;
cout << "What is your Phone Number?" << endl;
cin >> person.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> person.Email;
}

For the error you describe, you need to access members in the class instance, not the class definition ..
newPerson.Name
rather than
person.Name

Your mistakes were simply associated with syntax. Please read your compiler's error messages in the future.
#include <iostream>
#include <string> // added
using namespace std;
struct person {
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == 1) { // userselection is int so why compare it to char
person newPerson;
cout << "What is your Name?" << endl;
cin >> newPerson.Name; // assign to object's member not a static member
cout << "What is your Address?" << endl;
cin >> newPerson.Address;
cout << "What is your Phone Number?" << endl;
cin >> newPerson.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> newPerson.Email;
}
}

Related

Classes and OOP help C++

I'm having trouble with a simple code I have to for class, I'm trying to figure out how to add in a user input into a class, I've tried multiple things and could really use some assistance.
Code so Far:
#include <iostream>
#include <string>
using namespace std;
// Base class
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
// Derived class
class FamilyFued{
public:
points;
};
int main ()
{
GameShow TN;
TN.Name
return 0;
}
Classes are used to abstract the state and behavior of things.
State is in the form of class attributes.
The behaviour is in the form of class methods (functions).
This is a behaviour:
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
So you need to put it inside a function:
class GameShow{
public:
string Name;
GameShow(){
cout << "Enter name of contestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};
Based on what you have, you might want to wrap cout >> ...; cin <<...; part in a constructor:
class GameShow{
public:
string Name;
GameShow()
{
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};
Other than that, please refer to: The Definitive C++ Book Guide and List
You code missing too many basics, you need start with a good c++ book.
using namespace std; // is bad idea, mentioned in stack overflow thousands of times.
bad
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
Your cout and cin function should be used inside a constructor
class GameShow{
public:
string Name;
GameShow()
{
std::cout << "Enter name of constestant: ";
std::cin >> Name;
std::cout << "Welcome "
<< Name
<< "! Let's get ready to play the FEUD!!" << std::endl;
}
};
TN.Name // what this line doing in your main function ??
Assuming you wanted to print Name of GameShow class member, so change your line to below.
std::cout << TN.Name;

I'm trying to get the input for variables in private members of a class

this is my code
#include <iostream>
#include <string>
using namespace std;
class Hamoud{
private:
char name[50];
int yearbirthday;
public:
float tall;
int age;
void Getnameandyear(string name)
{
std::array<char, 50> ;
cout<<"enter your name: ";
getline(nama);
int year;
year = yearbirthday;
cout<<"enter your name: ";
getchar(name);
cout<<"Enter your year of birth";
cin>>year;
}
void display()
{
cout<<"your name is: "<<name;
cout<<"your year of birth is : "<<year;
}
};
int main ()
{
Hamoud info;
cout<<"enter your tall ";
cin>>info.tall;
cout<<"your tall is : "<<info.tall;
cout<<"enter your age: ";
cin>>info.age;
cout<<"your age is: "<<info.age;
info.Getnameandyear();
info.display();
}
but i got errors in the function getnameandyear also in display function as well...
i know that to access the private members of the class we have to create a function in public that will help us on access then indirectly....
However, I'm getting stuck on the last few steps..
Any idea of how to solve this problem ??
You probably want something like this:
#include <iostream>
#include <string>
using namespace std;
class Hamoud {
private:
string name;
int yearbirthday;
public:
float tall;
int age;
void Getnameandyear()
{
// no need for std::array<char, 50> or whatever here anyway
cout << "enter your name: ";
cin.ignore(); // this clears the input buffer. The exact reason why
// we need this is beyond your scope for the moment
getline(cin, name);
cout << "Enter your year of birth: ";
cin >> yearbirthday;
}
void display()
{
cout << "your name is: " << name << "\n";
cout << "your year of birth is: " << yearbirthday << "\n";
}
};
int main()
{
Hamoud info;
cout << "enter your tall ";
cin >> info.tall;
cout << "your tall is: " << info.tall << "\n";
cout << "enter your age: ";
cin >> info.age;
cout << "your age is: " << info.age << "\n";
info.Getnameandyear();
info.display();
}
But anyway, I think you should start reading good beginner's C++ text book.
About the cin.ignore(); before getline: the exact reason is a bit beyond your scope for the moment, but you can find detailed information about this issue in this SO article.

Why is my cout executing all at once in though cin should be read inbetween?

I am very new to C++ code and when I input anything but a number for any cin it automatically executes the rest of the cout.
Basically, if I wanted to name the first fruit "Apple", it would execute all of the other cout. If I named it only with numbers the other cout would be fine.
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Fruit {
char Name;
double Weight;
int Calories;
};
int main(){
struct Fruit TodaysFruit;
struct Fruit FruitA;
struct Fruit FruitB;
struct Fruit FruitC;
struct Fruit FruitD;
struct Fruit FruitE;
cout << "Enter Name For Fruit" << endl;
cin >> FruitA.Name;
cout << "Enter Weight For Fruit" << endl;
cin >> FruitA.Weight;
cout << "Enter Calories for Fruit" << endl;
cin >> FruitA.Calories;
//break
cout << "Enter Name For Fruit" << endl;
cin >> FruitB.Name;
cout << "Enter Weight For Fruit" << endl;
cin >> FruitB.Weight;
cout << "Enter Calories for Fruit" << endl;
cin >> FruitB.Calories;
//break
cout << "Enter Name For Fruit" << endl;
cin >> FruitC.Name;
cout << "Enter Weight For Fruit" << endl;
cin >> FruitC.Weight;
cout << "Enter Calories for Fruit" << endl;
cin >> FruitC.Calories;
return 0;
};
If you input "Apple" for
cin >> FruitA.Name;
Then "A" will go into the single character Name in your struct.
"pple" will remain in input stream and will fail to be read into all the following numbers, causing the input stream to go into error state and all subsequent reads will fail (credits Pete Becker).
To fix, replace
char Name;
with
std::string Name;
You need to #include <string> to support that (credits Ap31).
The std:: (a good input by Peter, credits) will make sure that the intended string is used, even if any active using would make confusion possible.

Identifier "friends_name" is undefined & the Identifier "input " is undefined? C++

I am trying to prompt the user to enter his or her first name.
I wrote "hello,first_name", where first_name is the name entered by the user.
Then I modified the code as follows: change the prompt to "enter the name of the person you want to write to" and change the output to "Hello, first_name,"following do you like where you are right now in life (y/n)?";
this Is right before I wrote an if statement, letting the user make the decisions on what code to execute, but the input in my a given "conditions" isn't working because its undefined despite me putting assigning them to 'y' and 'n'.
Long story short I don't understand entirely to how to define the first_name and the input, I'm trying to have the user input those values to be assigned to those variables.
The Error list
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main()
{
string first_name;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> first_name;
if (input =='y') {
cout << "what do you like about it over,the people?(y/n)";//?:\n"
}
else if (input == 'n') {
cout << " what do you hate about it,the expenses?(y/n)"//?:\n"
}
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
string = friends_name;
cin >> friends_name;
}
return 0;
}
}
In line 12 of your program it looks like you've used the variable first_name instead of input. Replace "first_name" with "input".
Eg:
cin>>input
Also as mentioned in the comments, make sure you declare all the variables that you use
Eg:
string first_name;
string input;
string friends_name;
Edited working code looks like:
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main(){
string first_name;
string friends_name;
string input;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> input;
if (input =="y")
cout << "what do you like about it over,the people?(y/n)";//?:\n"
else if (input == "n")
cout << " what do you hate about it,the expenses?(y/n)";//?:\n"
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
cin >> friends_name;
return 0;
}

Why does my code keep returning before it is complete?

Why does my code not run properly? As soon as it gets to the if else statement it takes one input from the user and then exits before I can enter anything else. I am not sure if it is due to the function not returning properly but I would really appreciate some help. Thanks.
#include <iostream>
#include <fstream>
using namespace std;
void studentenrollment (char answer);
int main()
{
char answer; //declaring variables for main
cout << "Welcome to Luton Sixth Form" << endl; //greeting the user
cout << "Please State if you are enrolled or not at the sixth form: Y/N" << endl;//giving user options
cin >> answer;//taking options from user
studentenrollment(answer); //calling student enrollment function
return 0;
}
void studentenrollment (char answer)
{
unsigned char name;
int dob;
if (answer=='Y'||answer=='y')
{
cout << "Welcome to higher education" << endl;
cout << "Please state your name" << endl;
cin >> name;
ofstream myfile;
myfile.open("StudentAccess.txt");
myfile << name << endl;
myfile.close();
cout << "Your name is now saved, you have access to the gateway" << endl;
}
else if(answer=='N'||answer=='n')
{
cout << "Please state your name" << endl;
cin >> name;
cout << "Please enter your date of birth" << endl;
cin >> dob;
ofstream myfile;
myfile.open("StudentEnrollment.txt");
myfile << name << dob << endl;
myfile.close();
cout << "You will now go through enrollment" << endl;
}
// return 0;
}
unsigned char name; looks incorrect. Choose char name[MAX_LENGTH]; or std::string name;
What happens
cin >> name; // Read just first character
cin >> dob; // Try to read number, where rest of the name is left in the stream buffer
This certainly looks wrong unless the name is 1 letter wide.
Problem can be this:
cin >> name;
you are entering name but storing it in name - which is just unsigned char. Use a larger array to store the name.
If the variable name can contain more than 1 character you cannot declare it unsigned char, you can declare it std::string. remember the
#include<string>