I have to make a program that calculates the population of the country each year .
in 2014 the populatin is x and in 2015 is x*(12%) and each year is incremented by 12%.
I've tryed to do this way but couldn't get through it:
#include<iostream>
using namespace std;
int main(){
int year;
double pop=344000
cout<<"Year of population: ";
cin >> year;
switch(year){
case 2014: cout<< "344000\n";
break;
case 2015: cout<< pop+=* 0.12 + pop ; //last year pop *0.12+ last year pop
break;
cout <<year;
}
system("pause");
return year;
}
I know its a mess but i really I'm noob in c++
increment next year`s population by 12 percent of last one , we check how many years is passed and increase the population with that no need for switch
int main(){
int year;
int baseYear=2014;
int dif;
int population=2000; //number of people in 2014
cout<<"Year of population: ";
cin >> year;
dif=year-baseYear
for(int i=0;i<dif;i++)
population+= ((0.12)*population)
cout << population;
}
I hope looking at this code will help you to understand what you are missing.
include<iostream.h>
void main()
{
int population= 3000000;
int year;
cout<<"Enter a year greater than or equal to 2014 ";
cin>>year;
if(year<2014)
cout<<"year must be greater than or equal to 2014";
elseif(year==2014)
cout<<population;
else
{
int i=2014;
while(i<=year)
{
population=population*1.12;
i++;
}
cout<<"population";
}
}
Related
I am new to C++, trying to import dates into a program, adding up digits of day, month, year resp and writing back to txt.
input data
sl.no name day month year
1 Rob 15 05 2019
2 Tim 12 06 2002
Desired output data in txt
sl.no name day month year
1 Rob 6 5 3
2 Tim 3 6 4
I have been able to import data from a txt file and also add the digits in day but it does not repeat forward. what am i doing wrong ?
sample code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream theFile("data.txt");
int id,day,month,year,daysum=0,monthsum=0, yearsum=0;
string name;
while (theFile >> id >> name >> day >> month >> year)
{
cout << id << ", "<< name <<", "<< day<<", "<<month <<", "<< year<<","<< endl;
}
while (day > 0)
{
daysum = daysum + (day % 10);
day = day / 10;
cout << daysum << endl;
}
I am no expert . but have been in your spot a few months ago.. break down the problem into smaller steps..
My approach..
Pseudo Code:
Ditch the header
Create a function for adding the digits
Read data from file
Use a loop to run through each element of the every column and use the function created
Store results in a variable
Output variable to a new text file
comment if there is a specific area where you are stuck..
Try this to reduce it to single digits.. knit to other parts of your code..
#include <iostream>
using namespace std;
int main()
{
long long num;
cout << "Enter a number: ";
cin >> num;
int sum = 0;
while (1)
{
sum += (num % 10);
num /= 10;
if (0 == num)
{
if (sum > 9)
{
num = sum;
sum = 0;
}
else
{
cout << "Answer: ";
cout << sum << endl;
return 0;
}
}
};
return 0;
}
you are reading the file and data wrong,
you need to discard the header (sl.no name day month year)
and then accumulate the daysum while reading the file progressively one row after the other until the end...
I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?
I am having problem in extracting the highest paid employee from empdetails.txt and finally displaying it to the user. i have completed getting the details from the user and merging the two files but for displaying highest paid using functions, i have no idea about it.
here is my code till now:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class emp
{
int num,age;
char name[20],dep[5];
public:
void getdata()
{
cout<<"\n\n Name = ";
cin>>name;
cout<<"\n Emp Num = ";
cin>>num;
cout<<"\n Department= ";
cin>>dep;
cout<<"\n Age = ";
cin>>age;
}
void display1()
{
cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
}
};
class sal
{
float gs,ns;
public:
void getsal()
{
cout<<"\n Gross sal = ";
cin>>gs;
cout<<"\n Net sal = ";
cin>>ns;
}
void display2()
{
cout<<"\t"<<gs<<"\t"<<ns;
}
};
void display()
{
emp e;sal s;
ifstream fil1;
fil1.open("empdetails.txt",ios::in);
cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";
while(!fil1.eof())
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
}
}
int main()
{
int n;
emp e1;sal s1;
ofstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::out);
fil2.open("sal.txt",ios::out);
fil3.open("empdetails.txt",ios::out);
cout<<"\n How many employee details do you want to enter = ";
cin>>n;
cout<<"\n Enter the deatils one by one \n";
for(int i=0;i<n;i++)
{
e1.getdata();
fil1.write((char*)&e1,sizeof(e1));
s1.getsal();
fil2.write((char*)&s1,sizeof(s1));
fil3.write((char*)&e1,sizeof(e1));
fil3.write((char*)&s1,sizeof(s1));
}
fil1.close();
fil2.close();
fil3.close();
cout<<"\n\n\t\t Merged file contents \n\n\t\t";
display();
getch();
return 0;
}
how can i make a function and what conditions to use?
You don't need a function, there's already one: std::max_element. It can figure out that you're working on class emp (the first two arguments to std::max_element. It can't figure out that you want employees sorted by salary, so that is the third argument that you have to provide: a function that takes two employees and which returns true if the first employee earns less than the second. (Sounds weird, but this allows you to use the same function for std::min_element)
for a class assignment I have to write a class definition. Its called Employee class, something really basic for my first c++ class.
My problem is in the first forloop, when I try to adjust the salary based on the new percentage.
The variables inside the class don't change after that. I don't know what could be wrong anymore.
The code is:
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int emplSalary;
string employeeName;
public:
Employee();
Employee(string name,int salary);
string getName();
int getSalary();
void newSalary(int percent);
void input();
void output();
};
Employee::Employee()
{
emplSalary=0;
employeeName="";
}
Employee::Employee(string name,int sal)
{
employeeName=name;
emplSalary =sal;
}
string Employee::getName()
{
return employeeName;
}
int Employee::getSalary()
{
return emplSalary;
}
void Employee::newSalary(int percent)
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
/*
if(percent < 0)
{
cout<<"Invalid Percentage";
cout<<"I calculated"<<endl;
}
else
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
}
*/
}
void Employee::input()
{
cout << "Enter Name: ";
cin>> employeeName;
cout<<"\n";
cout<<"Enter Salary: " ;
cin>>emplSalary;
cout<<"\n";
}
void Employee::output()
{
cout << "Name: " << employeeName <<" : "<< "Salary: " << emplSalary << endl;
cout<<"\n";
}
int main()
{
const int NUMBER_EMPLOYEE =1;
Employee employees[NUMBER_EMPLOYEE];
int percent;
cout<<"Welcome to Employee program. Enter Name and Salary when prompted."<<endl;
cout<<"\n";
cout<<"\n";
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i]=Employee();
employees[i].input();
cout<<"What percentage to raise the salary: ";
cin>>percent;
employees[i].newSalary(percent);
}
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i].output();
}
return EXIT_SUCCESS;
}
and the output is:
Welcome to Employee program. Enter Name and Salary when prompted.
Enter Name:
Enter Salary:
What percentage to raise the salary: I calculated
Name: : Salary: 0
The problem is in this line:
emplSalary= emplSalary *(1+(percent/100));
Because percent is an int, you are doing all integer math.
Suppose percent was 50.
The innermost part of your expression ends up being 50/100, which is 0 in integer math.
(You intended for the result to be 0.50).
To fix this, change the type of percent to be double.
Alternately, you can change 100 to be 100.0 (making it a double):
emplSalary= emplSalary *(1+(percent/100.0));
emplSalary= emplSalary *(1+(percent/100));
This line, if your percent is less than 99, percent/100 will be zero, that's why it has no affect to your result. You may want to use double type for your emplSalary and percent.
emplSalary= emplSalary *(1+(percent/100));
You're performing integer arithmetic there (emplSalaray and percent are both of type int). This means that percent / 100 will (unless percent is greater 99) always evaluate to 0. So the equation ends up being emplSalary = emplSalary * 1.
First, note that when the original salary is zero (which is what happens when you write employees[i]=Employee() and the default constructor sets the salary to 0), raises of any percentage will always remain zero,
Second, note that dividing an int by another int will perform integer arithmetic, so the quotient gets truncated. Raises between 0 and 100 percent will therefore be rounded to 0%. Divide by 100.0 instead of 100 to resolve that problem.
I would like to know how to input/initialize a start_date and end_date (which comes from a structure Date that has integers of month day and year from the function `initializeDate. Once I am able to initialize I assume I will be able to use the same logic in the printout member function.
struct Date
{
int month;
int day;
int year;
};
void initializeDate(Date &d)
{
cout<<"Please enter the month"<<endl;
cin>>start.month;
cout<<"Please enter the day"<<endl;
cin>>start.day;
cout<<"Please enter the year"<<endl;
cin>>start.year;
string dummy;
getline(cin, dummy);
}
edit: the error that I am getting is 'start' was not declared in this scope.
This is very basic, please read a good book on C++. Posting below because you have put in an effort :)
void Information::initializeDate(Date &d) //comes from the Information class.
{
// Commented as part of question change!
// Date d; // Guessing that the structure is the private member of the class.
cout<<"Please enter the month"<<endl;
cin>>d.month;
cout<<"Please enter the day"<<endl;
cin>>d.day;
cout<<"Please enter the year"<<endl;
cin>>d.year;
string dummy;
getline(cin, dummy);
}
** Just Edited the code as per your change in question
It looks like you keep updating the example code. Based on the current revision, I think this is what you want:
#include <iostream>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
void initializeDate(Date &date)
{
cout<<"Please enter the month"<<endl;
cin>>date.month;
cout<<"Please enter the day"<<endl;
cin>>date.day;
cout<<"Please enter the year"<<endl;
cin>>date.year;
}
int main()
{
Date start, end;
initializeDate(start);
initializeDate(end);
cout << start.year << "/" << start.month << "/" << start.day << endl;
cout << end.year << "/" << end.month << "/" << end.day << endl;
return 0;
};
Ok, there are a couple of problems here, that you should target. First, to fix your code, the error is very simple: there isn't anywhere in your code in which a variable named start was declared/defined. So, the compiler is asking you what start is. You are trying, I assume, to initialize the values of the members of d, that you passed in the function initializeDate, and all you have to do is just replace every occurence of the word start with d, and you'll get:
void initializeDate(Date &d)
{
cout<<"Please enter the month"<<endl;
cin>> d.month;
cout<<"Please enter the day"<<endl;
cin>> d.day;
cout<<"Please enter the year"<<endl;
cin>> d.year;
string dummy;
getline(cin, dummy);
}
Now, although this works, it's not the best way to initialize the date. Since Date is a struct, you can initialize its members using a constructor method. This is achieved by writing it like this:
struct Date{
int day, month, year;
Date(int, int, int);
};
Date:: Date(int day, int month, int year){
this->day = day;
this->month = month;
this->year = year;
}
int main(){
Date today(11, 3, 2013);
cout << "today, the date is " << today.day << "-" << today.month << "-" << today.year << endl;
return 0;
}