#include<iostream>
using namespace std;
int main()
{
int age;
char name;
cout<<"Enter age and name: ";
cin >> age >> name;
cout <<endl <<"your age: "<< age << endl << "name is: "<< name;
return 0;
}
What a run looks like:
Use a string instead of a char. A character only gets the first letter of the input.
The code should look like this.
#include<iostream>
using namespace std;
int main() {
int age = 0;
string name = "";
cout<<"Enter your age and name: ";
cin >> age >> name;
cout << endl;
cout << "Your age is " << age << endl;
cout << "Your name is " << name << endl;
return 0;
}
If you are required to use a character, you can try using a vector.
#include <iostream>
#include <vector>
using namespace std;
vector<char> _myStr;
void DisplayList () {
cout << "Your name is: ";
for (int i = 0; i < _myStr.size(); i++) {
cout << _myStr[i];
}
cout << endl;
}
void ConvertToVector (string theStr) {
for (int i = 0; i < theStr.length(); i++) {
_myStr.push_back(theStr[i]);
}
}
int main () {
int age = 0;
string name = "";
cout << "Enter your age and name: ";
cin >> age >> name;
cout << endl;
ConvertToVector (name);
cout << "Your age is: " << age;
DisplayList ();
return 0;
}
Use string instead of char...
#include<iostream>
#include <string>
using namespace std;
int main()
{
int age;
string name;
cout<<"Enter age and name: ";
cin >> age >> name;
cout <<endl <<"your age: "<< age << endl << "name is: "<< name;
return 0;
}
Related
I am new to programming, and I am currently trying to figure my way around C++. I am working on a program that will display basic information about employees to the users, and I want the program to end when the users entered a char. However, while working through it, I encountered a problem. When I enter the char (x), it somehow looped from 1-120 (as you seen in the counter below), but when I entered its ASCII number (120), it ended the program as intended. Can anyone explain it to me and what are the possible solutions?
#include <iomanip>
#include <iostream>
using namespace std;
class Employee
{
public:
int Age;
string Name;
int ID;
void information()
{
cout << "Name: " << Name << endl;
cout << "Age: " << Age << endl;
cout << "ID: " << setfill('0') << setw(3) << ID << endl;
}
Employee(string name, int age, int id)
{
Name = name;
Age = age;
ID = id;
}
};
void createProfile()
{
int age;
int id;
string name;
cout << "Enter the Employee Name: ";
cin >> name;
cout << "Enter the Employee Age: ";
cin >> age;
cout << "Enter the Employee ID: ";
cin >> id;
}
int main()
{
int num, counter;
const char stop = 'x';
cout << "Choose an Employee " << endl;
cout << "001 Jack" << endl;
cout << "002 Susan" << endl;
cout << "003 to create profile" << endl;
cout << "Press x to Exit";
for (num = 0; num != stop; num++)
{
cout << "\n\nEnter the Employee ID: ";
cin >> num;
counter += 1;
if (num == 001)
{
Employee jack = Employee("Jack", 25, 001);
jack.information();
}
else if (num == 002)
{
Employee jack = Employee("Susan", 23, 002);
jack.information();
}
else if (num == 003)
{
createProfile();
}
else
{
cout << "Input invalid";
}
}
cout << "\n" << counter;
}
Yes, of course. In C/C++, char is a type that can be represented either by its character value ('A', for example) or its numeric value (65). However, a digit's numeric value does not equal to itself.
'1' == 49
120 is the ASCII value of lower case x and since you loop with an int, it is logical that it will end up working in ways you do not expect it to work. You can change your
for (num = 0; num != stop; num++)
to
for (num = 0; ch != stop; num++)
and of course ch should be declared as char before your loop. Also, make sure that you replace cin >> num; with cin >> ch;
I am looking to remove the space from the name part of my assignment which allows users to put any name with or without space in my product list. for example "TV stand", I assume getline function will help me with it but I couldn't add the getline function into my main. can anyone help me with it?
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
struct product {
char product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
cin >> Products[i].product_name;
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
cin >> Products[i].no_of_sales;
To be honest I don't understand your question but this might be helpful for you
I changed the product_name data type from char to string because the char data type is not making any sense
#include <iostream>
#include <string>
using namespace std;
struct product {
string product_name;
int no_of_purchase;
int no_of_sales;
double purchase_cost;
double selling_price;
double profit_loss;
double percent_profit_loss;
string product_sales;
};
bool Compare(product P1, product P2)
{
return P1.percent_profit_loss > P2.percent_profit_loss;
}
int main()
{
int n;
cout << "Enter the number of the product:";
cin >> n;
cin.ignore();
cout << "\n";
product Products[n];
for (int i = 0; i < n; ++i) {
cout << "Enter the name of the product: ";
getline(cin, Products[i].product_name);
cout << "Enter the number of " << Products[i].product_name << " purchased: ";
cin >> Products[i].no_of_purchase;
cout << "Enter the number of " << Products[i].product_name << " sold: ";
getline(cin, Products[i].no_of_sales);
cin.ignore();
}
return 0;
}
I am tring to pop the data in the vector. But after printing code is not coming out
Why? What should be done to make it correct.
#include <iostream>
#include <vector>
using namespace std;
typedef struct add
{
string name;
string address;
}Address;
typedef struct st
{
vector<Address>madder;
}SLL;
int main()
{
SLL * st;
int n=3;
Address ad,rad;
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st->madder.push_back(ad);
}
while (!st->madder.empty())
{
rad = st->madder.back();
cout << rad.name << " " <<rad.address <<endl;
st->madder.pop_back();
}
}
You must allocate an object to be pointed at by st before dereferencing st.
Also you should delete what is allocated.
int main()
{
SLL * st;
int n=3;
Address ad,rad;
st = new SLL; // add this
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st->madder.push_back(ad);
}
while (!st->madder.empty())
{
rad = st->madder.back();
cout << rad.name << " " <<rad.address <<endl;
st->madder.pop_back();
}
delete st; // add this
}
Another option is not using the pointer and allocating the SLL object directly as variable.
int main()
{
SLL st;
int n=3;
Address ad,rad;
while(n--)
{
cout << "enter the name : ";
cin >> ad.name;
cout << "enter the adderess : ";
cin >> ad.address;
st.madder.push_back(ad);
}
while (!st.madder.empty())
{
rad = st.madder.back();
cout << rad.name << " " <<rad.address <<endl;
st.madder.pop_back();
}
}
I need to create a class of Students where I need to store their name, surname and subjects that they have in uni. How do I need to assign different subjects to different students? User should input it from console. What my class and input should look like? Heres is the code I have so far For example: there are two students Mark Jacobs and John Johny. Mark has 3 subjects: Math, Physics, Geography and John only has 2 Math and Economy.
#include <iostream>
using namespace std;
class Student{
private:
string name;
string surname;
int courseNo;
string subjects[100];
public:
Student(string name = "empty", string surname = "empty")
{
this->name = name + surname;
}
~Student(){
}
void setFullName(string name, string surname){
this->name = name;
this->surname = surname;
}
string getFullName(){
return name + " " + surname;
}
void setSubject(string subjects[100]){
this->subjects[90] = subjects[90];
}
string getSubject(){
return subjects[90];
}
string toString() const
{
return "Studentas: " + this->name;
}
};
int main() {
int number_of_students;
int number_of_subjects = 0;
int choice;
string subjects[90];
string name;
string surname;
cout << "Enter number of students: ";
cin >> number_of_students;
Student Studentas[number_of_students];
cout << "Enter " << number_of_students << " students names and surnames: " << endl;
for (int i = 0; i < number_of_students; i++) {
cin >> name >> surname;
Studentas[i].setFullName(name, surname);
}
for (int i = 0; i < number_of_students; i++) {
cout << i+1 << " Student: " << Studentas[i].getFullName() << endl;
}
cout << "Choose which students subjects you want to enter: ";
cin >> choice;
if(choice == 1){
cout << "Enter " << Studentas[choice-1].getFullName() << " number of subjects: ";
cin >> number_of_subjects;
for (int i = 0; i < number_of_subjects; i++) {
cin >> subjects[i];
Studentas[choice-1].setSubject(subjects);
}
cout << "Studying subjects are: " << endl;
for(int i = 0; i < number_of_subjects; i++){
cout << Studentas[choice-1].getSubject() << endl;
}
}
return 0;
}
I have taken your code changed a little bit, I hope this is what you are looking for
Code:
#include <iostream>
#include <vector>
using namespace std;
class Student {
private:
string name_;
string surname_;
int courseNo_;
std::vector<string> subject_list_;
public:
Student(string name = "empty", string surname = "empty")
{
name_ = name + surname;
}
void setFullName(string name, string surname) {
name_ = name;
surname_ = surname;
}
string getFullName() {
return name_ + " " + surname_;
}
void setSubject(string subject) {
subject_list_.push_back(subject);
}
const std::vector<string>& getSubjects() {
return subject_list_;
}
string toString() const
{
return "Student as: " + name_;
}
};
int main() {
std::vector<Student> student_list;
int number_of_students;
int number_of_subjects = 0;
int choice;
string subject;
string name;
string surname;
cout << "Enter number of students: ";
cin >> number_of_students;
cout << "Enter " << number_of_students << " students names and surnames: " << endl;
for (int i = 0; i < number_of_students; i++) {
cin >> name >> surname;
student_list.push_back(Student(name, surname));
}
int i = 1;
for (auto student : student_list) {
cout << i++ << " Student: " << student.getFullName() << endl;
}
cout << "Choose which students subjects you want to enter: ";
cin >> choice;
if (choice <= number_of_students) {
choice -= 1;
cout << "Enter " << student_list[choice].getFullName() << " number of subjects: ";
cin >> number_of_subjects;
for (int i = 0; i < number_of_subjects; i++) {
cin >> subject;
student_list[choice].setSubject(subject);
}
cout << "Studying subjects are: " << endl;
const auto& subject_list = student_list[choice].getSubjects();
for (auto& subject : subject_list){
cout << subject << endl;
}
}
return 0;
}
So I have this do-while loop which is supposed to repeat asking if you want to enter info for a student if you do press y and enter info via an input() function the data is then stored in a vector. If you press q, the program is supposed to print the info contained in the vector and exit the loop.
For some reason the loop fully executes for the first and second student you enter. Then instead of repeating the loop asking if you want to enter a third student it seems to just execute the input() function. It doesn't ask you 'Enter y to continue or q to quit' and any student info you enter here does not seemed to be stored. It does this intermittently changing between executing the full loop and just the input() function. I'm wondering if anyone knows why this is happening and what I can do to fix it.
Cheers
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int NO_OF_TEST = 4;
struct studentType
{
string studentID;
string firstName;
string lastName;
string subjectName;
string courseGrade;
int arrayMarks[4];
double avgMarks;
};
studentType input();
double calculate_avg(int marks[],int NO_OF_TEST); // returns the average mark
string calculate_grade (double avgMark); // returns the grade
void main()
{
unsigned int n=0; // no. of student
vector<studentType> vec; // vector to store student info
studentType s;
char response;
do
{
cout << "\nEnter y to continue or q to quit... ";
cin >> response;
if (response == 'y')
{
n++;
for(size_t i=0; i<n; ++i)
{
s = input();
vec.push_back(s);
}
}
else if (response == 'q')
{
for (unsigned int y=0; y<n; y++)
{
cout << "\nFirst name: " << vec[y].firstName;
cout << "\nLast name: " << vec[y].lastName;
cout << "\nStudent ID: " << vec[y].studentID;
cout << "\nSubject name: " << vec[y].subjectName;
cout << "\nAverage mark: " << vec[y].avgMarks;
cout << "\nCourse grade: " << vec[y].courseGrade << endl << endl;
}
}
}
while(response!='q');
}
studentType input()
{
studentType newStudent;
cout << "\nPlease enter student information:\n";
cout << "\nFirst Name: ";
cin >> newStudent.firstName;
cout << "\nLast Name: ";
cin >> newStudent.lastName;
cout << "\nStudent ID: ";
cin >> newStudent.studentID;
cout << "\nSubject Name: ";
cin >> newStudent.subjectName;
for (int x=0; x<NO_OF_TEST; x++)
{ cout << "\nTest " << x+1 << " mark: ";
cin >> newStudent.arrayMarks[x];
}
newStudent.avgMarks = calculate_avg(newStudent.arrayMarks,NO_OF_TEST );
newStudent.courseGrade = calculate_grade (newStudent.avgMarks);
return newStudent;
}
double calculate_avg(int marks[], int NO_OF_TEST)
{
double sum=0;
for( int i=0; i<NO_OF_TEST; i++)
{
sum = sum+ marks[i];
}
return sum/NO_OF_TEST;
}
string calculate_grade (double avgMark)
{
string grade= "";
if (avgMark<50)
{
grade = "Fail";
}
else if (avgMark<65)
{
grade = "Pass";
}
else if (avgMark<75)
{
grade = "Credit";
}
else if (avgMark<85)
{
grade = "Distinction";
}
else
{
grade = "High Distinction";
}
return grade;
}
I think this code does it:
n++;
for(size_t i=0; i<n; ++i)
{
s = input();
vec.push_back(s);
}
It asks you for two students the second time, for three student the third time, etc.
So, make it
vec.push_back(input());