Accelerated C++ (Section 3.2.1: vector<double> homework) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In section 3.2.1 - Storing a collection of data in a vector on page 42 of the book Accelerated C++, I came across an error in my code after following what it had told me to type.
// revised version of the excerpt
double x;
vector<double> homework;
// invariant: homework contains all the homework grades read so far
while (cin >> x)
homework.push_back(x);
I understand the concept of vectors, but I simply don't understand why my code is giving me an error messages specifically pointing at the
vector<double> homework;
declaration. Does C++11 and C++14 not support this declaration for vectors anymore?
Here is my exact code:
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <ios>
#include <string>
using std::cin; using std::string;
using std::cout; using std::setprecision;
using std::endl; using std::streamsize;
int main()
{
// ask for and read the student's name
cout << "\n Please enter your first name: ";
string name;
cin >> name;
cout << " Hello, " << name << "!" << endl;
// ask for and read the midterm and final grades
cout << " Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << " Enter all your homework grades, "
" followed by end-of-file: ";
//the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
vector<double> homework;
/*invariant:
we have read COUNT grades so far, and
SUM is the sum of the first COUNT grades*/
while (cin >> x) {
homework.pushback(x);
}
// write the result
streamsize prec = cout.precision();
cout << " Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}

std::vector resides in the header <vector>. In order to use it that header needs to be included in your code. To do that you need to #include <vector>. Then you will also need to have a using std::vector; or use std::vector.

Related

if statement facing error in percentage C++ code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have faced the problem the formula of percentage doesn't work properly
#include <iostream>
using namespace std;
int main() {
int tmarks,intermarks, passmarks;
float per;
cout << "Enter Your Inter Marks:\n";
cin >> intermarks;
cout << "Enter Your Total Marks:\n";
cin >> tmarks;
cout << "Enter Your PassMarks:\n";
cin >> passmarks;
per = (intermarks/tmarks) * 100;
cout << "percentage:" << per;
if (per >= 45 && passmarks >= 50) {
cout << "Welcome To Uni\n";
} else {
cout << "Improve Your Marks You are eligible\n";
}
}
If intermarks = 50 and tmarks = 75, then intermarks/tmarks will be 0. Since both are integers. You need to typecast before division operation. This way float(intermarks) / float(tmarks) will be 0.67 and per will be 67

No operator ">>" matches these operands [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I've done some digging on this question and have found other people with similar, but non-identical errors to me. My two top theories are that I'm missing something obvious or I've broken Visual Studio. The code runs as follows:
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int child;
int adult;
int costs;
string movie;
int profits;
std::cout >> "What is the name of the movie? ";
std::getline(cin, movie);
std::cout >> "How many kids went to the movie? ";
std::cin << child;
std::cout >> "how many adults went to the movie? ";
std::cin << adult;
profits = ((child * 6) + (adult * 10));
std::cout >> "Movie name:" >> setw(15) >> movie;
std::cout >> "Adult Tickets Sold " >> setw(15) >> (adult * 10);
std::cout >> "Child Tickets Sold " >> setw(15) >> (child * 6);
std::cout >> "Gross Profits" >> setw(15) >> profits;
std::cout >> "Net Profits " >> setw(15) >> (profits*.2);
std::cout >> "Amount paid to distributor " >> setw(15) >> (profits - (profits*.2));
return 0;
}
Every instance of >> and << are red underlined with the error messages:
No operator '>>' matches these operands
Identifier 'setw' is undefined
I'm quite sure that I've done something glaringly obvious and wrong, but I can't find it for the life of me.
You got >> and << reversed. << is for std::cout and >> is for std::cin. You are doing the opposite. Also you need to include iomanip for std::setw.
<< is a stream insertion operator, this is used with an ostream object which is cout. >> is a stream extraction operator, which is used with and istream object cin. In your program you have clearly exchanged their places. Fix it, and then everything will work smooth.
Moreover, you have written the statement, using namespace std, then there is no need to use specify the namespace again. I mean either change std::cout (and all other similar lines) to cout or just remove the line using namespace std;. However, the latter is a better choice.

Why the result differs when I press enter at the end of a sentence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Here is my first program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a;
string s;
double d;
while(cin >> a >> s >> d)
cout << a << s << d;
return 0;
}
When I input some simple data and press Enter , the result is shown immediately:
However, the code in another program behaves differently:
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double price = 0.0;
void Print();
};
void Sales_data::Print(){//print every record of the Sales_data
cout << "The bookNo of the book is " << bookNo << endl;
cout << "The units_sold of the book is " << units_sold << endl;
cout << "The price of the book is " << price << endl;
}
int main()
{
Sales_data book;
while(cin >> book.bookNo >> book.units_sold >> book.price);
book.Print();
return 0;
}
When I run this code, input some data, and press Enter, it waits for me to input more data rather than show the result.
Could you explain this to me?
Remove the semicolon after the while loop. As it is, it forces the loop to have no body, which means it just cycles over the cin forever. Even better, use braces to delimit the body:
while(cin >> book.bookNo >> book.units_sold >> book.price) {
book.Print();
}

Dynamic array problems [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Ok my problem is, in my dynamic array function I have an array that gives me the error below.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
class Revenue
{
};
static int Track_Num_Divisions = 1;
static int Track_Quart_Revenue = 1;
void Program_loop()
{
{
string D;
string DN;
int N;
double TS;
double TC;
double P;
int arry;
cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue* rev = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i < arry; i++,Track_Num_Divisions++ )
{
Revenue& rev = rev[i];// THIS IS THE ERROR <<<<
cout << " " << endl;
cout << "Revenue #"<<Track_Num_Divisions << endl;
cout << "===========" << endl;
cout << "<< Ok what is your division name?: " << endl; cin >> D;
string set_Division_name(D);
cout << "<< What is your division number?: " << endl; cin >> DN;
string set_Division_number(DN);
while (DN.size() != 4)
{
cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
}
delete[] rev;
}
Gives this error in function Dynamic_array
I think the problem lies in this code>> Revenue& rev =rev[i]:
Error 1 error C2676: binary '[' : 'Revenue' does not define this operator or a conversion to a type acceptable to the predefined operator
Error 2 IntelliSense: no operator "[]" matches these operands
operand types are: Revenue [ int ]
What should I do?
I am kinda new to this Website still learning the ropes of proper format.
This line is the problem:
Revenue& rev = *rev[i];
You're dereferencing the value returned by rev[i], but rev[i] is not a pointer or a class with an overloaded operator*. It's a Revenue&.
There's no need for derferencing anything here, just write it as:
Revenue& rev = rev[i];

Make a C++ program ask something many times [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can someone help me? I want to make a program that asks me "Whats the first?", "Whats the second?", "Whats the third?" etc. I want it to ask me that as many times as I have told it. It is stored in the int noftimes.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Get number of grades.
int nofgrades = 7;
cout << "How many grades do you want to calculate?\n";
cin >> nofgrades;
//Get grades.
cout << "What's the " << (nofgrades - (nofgrades - 1)) << "st grade?\n";
//Calculate average.
//Display average.
return 0;
}
I'm programming in C++. All help is appreciated.
Here you go, by using for loop it's simple to do what you ask. Read a bit more about for loops and general programming principles. Look at this subreddit sidebar for good resources.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Get number of grades.
int nofgrades; // Why did you initialize it when you're going to override it anyway?
cout << "How many grades do you want to calculate?\n";
cin >> nofgrades;
//Get grades.
for (int i = 1; i <= nofgrades; ++i) {
cout << "What's the " << i << "st grade?\n";
//Load aditional grades...
}
//Calculate average.
//Display average.
return 0;
}