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

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;
}

Related

How to repeat the string in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have to write a program in which integer value is entered from user and the string has to be displayed that many times. But I am getting errors.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
cout << string(N, "Well Done");
return 0;
}
Note: I am not permitted to use a loop in this assignment.
If you may not use a loop, you may use goto to get around the restriction:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
{
int i = 0;
goto test;
begin:
cout << "Well Done";
++i;
test:
if (i < N)
goto begin;
}
return 0;
}
Note that goto is widely considered bad practice.
EDIT2: IN THE ORIGINAL ASKER's COMMENTS, LOOPS OF ANY KIND ARE PROHIBITED IN THIS ASSIGNMENT.
Use recursion.
void printN(int n, string s) {
if (n <= 0) return;
cout << s << endl;
printN(n-1, s);
}
Then you can call this from your main program as follows:
printN(userInput, "Hi my name is ricky bobby");
EDIT: just saw you haven't learned recursion yet. Look up this term, and familiarize yourself with it. This is a way to do iteration without looping (this is the most simplistic way I can describe it)
std::string does not have a constructor that repeats a string N times (it does have one for repeating a single character N times, though). What you need is a loop instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
for (int i = 0; i < N; ++i)
cout << "Well Done";
return 0;
}

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

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();
}

Accelerated C++ (Section 3.2.1: vector<double> homework) [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 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.

C++ do while loop [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 9 years ago.
Improve this question
I tried to make a program to get the amount after certain days of investment at some rate.my try was the below program it didnt gave any errors but also it didnt gave the result.please help.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a;
float p;
float r ;
int days;
int day;
cout << "Enter the a principal amount"<<endl;
cin >>p;
cout <<"Enter the rate "<<endl;
cin >>r;
cout << "Enter number of days"<<endl;
cin>> days;
do(){
a= p* pow(1+r,day);
cout << day << "-------"<<a<<endl;
day++;
}
while (day <=days);
}`
1.You haven't initialized 'day' variable.
----> day=1;
2.The syntax of do while is wrong.
do{
}while(condition);