I'm trying to read characters from std::cin using a char[] array.
Here is the simple program:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
int age, years;
char name[20];
cout << "Enter your age in years: " << endl;
cin >> years;
cout << "Enter your name in years: " << name[15] << endl;
age = years * 12;
cout << " Your age in months is: " << age << endl;
cout << "Your name is: " << name[15] << endl;
return 0;
}
And here what i get as an output
Enter your age in years:
19
Enter your name in years:
Your age in months is: 228
Your name is:
It doesn't recognizing the array from std::cin.
Anyone can help?
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main(){
int age , years ;
char name[20];
cout <<"Enter your age in years: "<< endl;
cin >> years;
cout <<"Enter your name in years: " <<endl;
cin >> name;
age = years*12;
cout << " Your age in months is: " << age <<endl;
cout << "Your name is: "<< name <<endl;
return 0;
}
There are two differences with your code:
cin << name;
(...)
cout << name << endl;
I'm assuming you thought
cout << "Enter your name" << name[15] << endl;
would make it ask for input. This is not what cout does. Cout prints out stuff on the console, it doesn't ask for input. That's cin's task.
Also you don't put [15] after the array name, this would just print out the 15th character in your array, which would be a garbage character as long as the name entered does not reach a length of 15.
int age, years;
char name[20];
cout << "Enter your age in years: " << endl;
cin >> years;
cout << "Enter your name in years: " << endl;
age = years * 12;
cin >> name;
cout << " Your age in months is: " << age << endl;
cout << "Your name is: " << name << endl;
cin.get();
Related
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
string userMiles;
string userSteps;
//declaring constant for number of steps/mile
const int stepsPerMile = 2000;
//getting user's name
cout << "What is the user's name?";
cin >> userName;
cout << endl;
//getting miles walked
cout << "How many miles did " << userName << " hike today?";
cin >> userMiles;
cout << endl;
//getting other steps taken
cout << "How many other steps did " << userName << " take today?";
cin >> userSteps;
cout << endl;
string stepsTakenFromMiles;
string totalSteps;
stepsTakenFromMiles = userMiles * stepsPerMile;
totalSteps = stepsTakenFromMiles + userSteps;
cout << userName << " took " << totalSteps << " steps throughout the day." << endl;
return 0;
}
I am trying to multiply userMiles (string) by stepsPerMile (const int) and I keep getting an error that they are not matching operand types. I have to use stepsPerMile as a const int and cannot change it. How do I change my code to allow both of these inputs to be multiplied?
Read in userMiles and userSteps as integers, not as strings. operator>> can read in many different types, not just std::string. Try using int instead, to match your stepsPerMile constant, eg:
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
int userMiles;
int userSteps;
//declaring constant for number of steps/mile
const int stepsPerMile = 2000;
//getting user's name
cout << "What is the user's name?";
cin >> userName;
cout << endl;
//getting miles walked
cout << "How many miles did " << userName << " hike today?";
cin >> userMiles;
cout << endl;
//getting other steps taken
cout << "How many other steps did " << userName << " take today?";
cin >> userSteps;
cout << endl;
int stepsTakenFromMiles;
int totalSteps;
stepsTakenFromMiles = userMiles * stepsPerMile;
totalSteps = stepsTakenFromMiles + userSteps;
cout << userName << " took " << totalSteps << " steps throughout the day." << endl;
return 0;
}
Demo
a nice program I checked it out. Cool program, I like it. I think to solve this problem. Instead of making userMiles a string, make it an int like this
#include <iostream>
#include <string>
using namespace std;
int main() {
//declaring cin inputs
string userName;
int userMiles;
string userSteps;
//declaring constant for number of steps/mile
const int stepsPerMile = 2000;
//getting user's name
cout << "What is the user's name?";
cin >> userName;
cout << endl;
//getting miles walked
cout << "How many miles did " << userName << " hike today?";
cin >> userMiles;
cout << endl;
//getting other steps taken
cout << "How many other steps did " << userName << " take today?";
cin >> userSteps;
cout << endl;
string stepsTakenFromMiles;
string totalSteps;
stepsTakenFromMiles = userMiles * stepsPerMile;
totalSteps = stepsTakenFromMiles + userSteps;
cout << userName << " took " << totalSteps << " steps throughout the day." << endl;
return 0;
}
Just change your identifiers
int userMiles, userMiles;
because in C++ you can't use the multiplication operator with strings, this can be actually used in other programming languages such as python like when you multiply a string by a number the string will be repeated but in C++ you can't use arithmetic operators with string except "+" which can be used to join 2 or more strings.
This is a c++ course for beginners check it hope it helps
sorry I am relatively new to c++ and am currently stuck. The point of the application is to have the user enter the number of employees they have and then information about their employees including the hours they worked and their pay rate. After that that application to print out all the information and then give them each employees gross pay. I thought I had everything set up correctly but am getting an error on line 26 it is saying "expression must have constant value". Any tips or advice would be appreciated.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
struct Employee
{
int id;
string fName;
string lName;
int pay;
int hours;
};
int main() {
int i, n;
cout << "Enter number of employees";
cin >> n;
Employee Emp[n];
for (i = 0; i < n; i++) {
cout << "Enter Employee ID: ";
cin >> Emp[i].id;
cout << "Enter First Name: ";
cin >> Emp[i].fName;
cout << "Enter Last Name: ";
cin >> Emp[i].lName;
cout << "Enter in Pay Rate: ";
cin >> Emp[i].pay;
cout << "Enter in Hours: ";
cin >> Emp[i].hours;
}
cout << "\n*** Employee Details ***";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";
for (i = 0; i < n; i++) {
cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
}
_getch();
return 0;
}
Employee Emp[n];
In C/C++ you can't declare dynamic-size arrays like this.
See this question - How to create a dynamic array of integers
Or better, use an std::vector instead.
C++ standard requires you to provide an array size known at compile time.
Therefore to acquire what you want you need to use dynamic memory allocation i.e. allocate an array on heap depending upon the n being entered by the user. The following demonstrates this method.
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
struct Employee
{
int id;
string fName;
string lName;
int pay;
int hours;
};
int main() {
int i, n;
cout << "Enter number of employees";
cin >> n;
auto *Emp = new Employee[n];
for (i = 0; i < n; i++) {
cout << "Enter Employee ID: ";
cin >> Emp[i].id;
cout << "Enter First Name: ";
cin >> Emp[i].fName;
cout << "Enter Last Name: ";
cin >> Emp[i].lName;
cout << "Enter in Pay Rate: ";
cin >> Emp[i].pay;
cout << "Enter in Hours: ";
cin >> Emp[i].hours;
}
cout << "\n*** Employee Details ***";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << setw(10) << "Pay" << setw(10) << "Hours" << setw(10) << "Gross Pay";
for (i = 0; i < n; i++) {
cout << "\n" << Emp[i].id << setw(15) << Emp[i].fName << setw(10) << Emp[i].lName << setw(10) << Emp[i].pay << setw(10) << Emp[i].hours << setw(10) << Emp[i].pay*Emp[i].hours;
}
delete [] Emp;
return 0;
}
Okay, so I am writing a C++ program to declare a struct data type that holds the following information on an employee (First Name, Last Name, ID, Pay Rate, and Hours). My problem is that the user can only enter in the ID and First Name, then the whole program runs without letting the user enter the rest of the data.
Heres my code:
#include <iostream>
#include <iomanip>
using namespace std;
struct Employee
{
int employeeID;
char firstName;
char lastName;
float payRate;
int hours;
};
int main()
{
int i, j;
cout << "How Many Employees Do You Wish To Enter?:\n\n";
cin >> j;
Employee info;
for (i = 0; i < j; i++)
{
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >> info.employeeID;
cout << setw(5) << "\n Please Enter Employees First Name: ";
cin >> info.firstName;
cout << setw(5) << "\n Please Enter Employees Last Name: ";
cin >> info.lastName;
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >> info.payRate;
cout << setw(5) << "\n Please Enter The Hours The Employee Worked:
";
cin >> info.hours;
}
cout << "\n\n \n";
cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" <<
setw(10) << "Pay Rate" << setw(10) << "Hours";
cout << endl;
for (i = 0; i < j; i++)
{
cout << "\n" << info.employeeID << setw(15) << info.firstName << setw(10) << info.lastName << setw(10) << info.payRate << setw(10) << info.hours;
}
cout << "\n\n \n";
system("pause");
return 0;
};
#include <iostream>
#include <iomanip>
#include <string> //Allows you to use strings, which are way more handy for text manipulation
#include <vector> //Allows you to use vector which are meant to be rezied dynamicaly, which is your case
using namespace std;
struct Employee
{
int employeeID;
string firstName; //HERE : use string instead of char (string are array of char)
string lastName; //HERE : use string instead of char
float payRate;
int hours;
};
int main()
{
int j;
cout << "How Many Employees Do You Wish To Enter?:\n\n";
cin >> j;
vector<struct Employee> info; //creation of the vector (dynamic array) to store the employee info the user is going to give you
for (int i = 0; i < j; i++) //declare your looping iterator "i" here, you will avoid many error
{
struct Employee employee_i; // create an employee at each iteration to associate the current info
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << "\n Please Enter The Employee ID Number: ";
cin >> employee_i.employeeID;
cout << "\n Please Enter Employees First Name: ";
cin >> employee_i.firstName;
cout << "\n Please Enter Employees Last Name: ";
cin >> employee_i.lastName;
cout << "\n Please Enter Employees Pay Rate: ";
cin >> employee_i.payRate;
cout << "\n Please Enter The Hours The Employee Worked: ";
cin >> employee_i.hours;
info.push_back(employee_i); //store that employee info into your vector. Push_back() methods expands the vector size by 1 each time, to be able to put your item in it
} // because you employee variable was create IN the loop, he will be destruct here, but not the vector which was created outside
cout << "\n\n \n";
for (int i = 0; i < j; i++) //the loop to get back all the info from the vector
{
cout << "ID :" << info[i].employeeID << " First Name :" << info[i].firstName << " Last Name :" <<
info[i].lastName << " Pay Rate :" << info[i].payRate << " Hours :"<< info[i].hours;
cout << endl;
//notice the info[i], which leads you to the employee you need and the ".hours" which leads to the hours info of that specific employee
}
system("pause");
return 0;
}
First, please read Tips and tricks for using C++ I/O (input/output). It might be helpful to understand C++ I/O.
Here are some comments on your code:
First
Use string instead of char.
struct Employee
{
int employeeID;
string firstName;
string lastName;
float payRate;
int hours;
};
Second
Use array of Employee object to store multiple employees.
Employee info[100];
Third
Use cin carefully depending data types. In your case, it would be something like this:
cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >> info[i].employeeID;
cin.ignore(); //It is placed to ignore new line character.
cout << setw(5) << "\n Please Enter Employees First Name: ";
getline (cin, info[i].firstName);
cout << setw(5) << "\n Please Enter Employees Last Name: ";
getline (cin, info[i].lastName);
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >> info[i].payRate;
cout << setw(5) << "\n Please Enter The Hours The Employee Worked: ";
cin >> info[i].hours;
Fourth
std::getline() can run into problems when used before std::cin >> var. So, std::cin.ignore() can be used in this case to solve the problem.
I hope it helps.
I need help understanding the [Error] id return 1 exit status due to 2 undefined references: getGrades() and getAverage(). I was issued DEV C++ and knocked out the syntax errors with mild frustration but these "linking" errors are still giving me a hard time. This is the most recent update of the code, if anyone could help me understand these linking errors that would be great.
Compiler - Dev C++
Windows 7
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
// Function declarations
string getStudentName();
string getWork();
int getGrades();
double getAverage();
int main()
{
string studentName, work[3];
int grades[3];
double average;
// Get the name of the student
studentName = getStudentName();
// Get the work
work[3] = getWork();
// Get the grades
grades[3] = getGrades();
// Get the average
average = getAverage();
// Dynamic spacing for grades
ostringstream ss;
int gradesLength = ss.str().length();
ss <<setprecision(0) << fixed << showpoint;
ss << grades;
cout << "\n the average for " << studentName << " is: " << average << endl;
cout << "The grades for " << studentName << " are: " << endl;
cout << setw(30) << work[0] << ": " << setw(gradesLength) << grades[0] << endl;
cout << setw(30) << work[1] << ": " << setw(gradesLength) << grades[1] << endl;
cout << setw(30) << work[2] << ": " << setw(gradesLength) << grades[2] << "\n\n\n";
cout << "You have completed the program: \n";
return 0;
}
// Student Name
string getStudentName()
{
string name;
cout << "Enter students full name: ";
getline(cin, name);
return name;
}
// Assignments
string getWork()
{
string work[3];
cout << "\nEnter the name of each assignment \n";
cout << "First assignment: ";
getline (cin, work[0]);
cout << "Second assignment: ";
getline (cin, work[1]);
cout << "Third assignment: ";
getline (cin, work[2]);
return work[3];
}
// Grades
int getGrades(string work[3])
{
int grades[3];
cout << "\nEnter the grade for " << work[0] << ": ";
cin >> grades[0];
cout << "Enter the grade for " << work[1] << ": ";
cin >> grades[1];
cout << "Enter the grade for " << work[2] << ": ";
cin >> grades[2];
return grades[3];
}
// Math
double getAverage(int grades[3])
{
double average;
average = (grades[0] + grades[1] + grades[2]) / 3.0f;
return average;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
double age;
double months;
string name;
months = age*12.0;
cout << "Enter your name and age: ";
cin >> name >> age;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
return(0);
}
The program asks for name and age, and should out put the name and age in a sentence with the age in months in parentheses.
Output gives something like:
Hello Bob age 20 (1.82561e-313 months), but should be Hello Bob age 20 (240 months). I did not use int because I wanted to be able to input non int values for age.
I have tried 12 instead of 12.0 and tried declaring a variable and doing months = age*m where m = 12.0 but is result is the same. By the way, the random value is about the same regardless of what variable age is. Why is this happening? Also, would this be a link-time error or run-time error?
You're doing the multiplication using age before it's initialized. What value would you expect it to give you?
Change your code to first get the age, and then do the multiplication:
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";
Move the calculation of months to after the point age is entered (age was uninitialised in the original code):
cout << "Enter your name and age: ";
cin >> name >> age;
months = age*12.0;
cout << "Hello " << name << " age " << age << " (" << months << " months)\n";