I have this code which is giving me error, I have no idea how to fix it.
I don't know which part I'm doing wrong.
I have made a code like this but with out giving user a option,
for example:
int counter;
for (counter=1; counter<=100000; counter=counter+1)
{
cout<<"2x"<<counter<<"="<<2*counter<<"\n";
this code is giving me a table of 2.
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{ int counter, number, maxMultiplier;
{
cout>> "Please enter the number for which you want a table: ";
cin<< number;
cout>> "Please select the multiplier up to which you want a table: ";
cin>> maxMultiplier;
}
for (counter=1; counter<=maxMultiplier; counter=counter+1)
{ cout<<number<<"x"<<counter<<"="<<number*counter<<"/n";
}
{system("pause");}
return main();
}
The user should be able to enter the number for table they want and how long they wants a table to be. Like all the way up to 2x1=2.........2x10=20
after cleaning the code and taken into account the remarks of TypeIA and me :
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int number, maxMultiplier;
cout << "Please enter the number for which you want a table: ";
cin >> number;
cout << "Please select the multiplier up to which you want a table: ";
cin >> maxMultiplier;
for (int counter = 1; counter<=maxMultiplier; ++counter)
{
cout<<number<<"x"<<counter<<"="<<number*counter<<"\n";
}
system("pause"); // I don't like, pause does not exist under Linux etc
return 0;
}
Related
What is wrong with the for loop that is getting cname and camount from the user.
Here is my input:
size = 3
Name = "Asha R"
It throws me out of the loop and exits the program. I should be able to enter 3 names and 3 amounts based on the size I entered in the first statement.
I've tried to change the cin statements several different ways, but nothing is working. Your help is much appreciated:
#include <iostream>
#include <string>
using namespace std;
struct contribution {
string cname;
double camount = 0.0;
};
int main ()
{
int size = 0;
//get array size from user
cout << "How many members do you want to enter" << endl;
cin >> size;
//create dynamic array of contribution structure
contribution * conarr = new contribution[size];
//This is my problem area. When I enter the 1st loop and enter "Asha R" it kicks out of the loop. I tried to change the cin statements several different ways, but it's not working.
for (int i=0; i < size; i++)
{
cout << "Enter name: " ;
//getline(cin, conarr[i].cname);
getline(cin,conarr[i].cname).get();
cout << "Enter contribution amount: ";
// cin >> conarr[i].camount;
(cin >> conarr[i].camount).get();
}
delete[] conarr;
}
So I took a C++ class in High School, but haven't done it in years, so I'm basically new. So I am wondering if there is a limit to the amount of cins you can do. It allows me to input 7 and then skips all the other inputs to go to the end. My guess is that there's a data limit. Is this correct?
#include <iostream>
using namespace std;
int main() {
int time1;
int time2;
int time3;
int time4;
int time5;
int time6;
int time7;
int time8;
int time9;
int time10;
cout<<"enter number";
cin>>time1;
cout<<"enter number";
cin>>time2;
cout<<"enter number 1";
cin>>time3;
cout<<"enter number 1";
cin>>time4;
cout<<"enter number 1";
cin>>time5;
cout<<"enter number 1";
cin>>time6;
cout<<"enter number 1";
cin>>time7;
cout<<"enter number 1";
cin>>time8;
cout<<"enter number 1";
cin>>time9;
cout<<"enter number 1";
cin>>time10;
cout<<"the end?";
}
You can use cins unlimited and you need to use loops. Here I provided example of fillind array buy cin.
#include <iosteam>
using namespace std;
int main(){
cout<<"Enter number of cins\n";
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cout<<"Enter "<<i<<" number\n";
cin>>a[i];
}
}
Your code should work fine if you only enter numbers. There is no limit to the amount of cins you can do. My guess is that you enter something that isn't a number, e.g. a string. When that happens cin's error flag is set and future attempts to get input will fail.
What you can do is add some input validation if you want. A simple if statement will do:
int num{0};
if (cin >> num)
{
....
}
If the input is not valid, i.e. the if condition is false, you need to clear the error:
cin.clear();
and discard everything remaining in the input buffer and newline:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
You could use a std::vector to store the numbers instead of separate integer variables and use a do...while loop. Putting this all together your code could look like this:
#include <iostream>
#include <vector>
#include <limits>
int main()
{
std::vector<int> nums;
int n{0};
std::cout << "Enter total number to input" << std::endl;
std::cin >> n;
int i{0};
do
{
std::cout << "Enter number " << i+1 << std::endl;
int num{0};
if (std::cin >> num)
{
nums.push_back(num);
++i;
}
else
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input" << std::endl;
}
} while (i < n);
}
Note that I'm not using namespace std in my code. That's considered bad practice.
#include <iostream>
using namespace std;
int main ()
{
int quantity;
again:
cout<<"Enter the quantity: ";
cin>>quantity;
If(quantitiy == '1' )
{
total = quantity*price
}
else
{
goto again;
}
return 0;
}
How could I declare all the numbers in one if statement?
Pretty much everything in your code is wrong and needs fixing. Use a do..while loop instead of goto. Correct your typos on If and quantitiy. Declare and initialize the missing total and price variables. Add the missing ; on the total assignment. And '1' and 1 are two completely different types and values.
Try something more like this instead:
#include <iostream>
using namespace std;
int main ()
{
int quantity;
double price = ...; // for you to decide, unless you ask the user...
double total = 0;
do {
cout << "Enter the quantity: ";
cin >> quantity;
}
while (quantity != 1);
total = quantity*price;
return 0;
}
Of course, it really doesn't make sense to prompt the user for a quantity and then only accept 1. You probably want something more like this:
#include <iostream>
using namespace std;
int main ()
{
int quantity;
double price = ...; // for you to decide, unless you ask the user...
double total = 0;
do {
cout << "Enter the quantity (0 to stop): ";
cin >> quantity;
if (quantity == 0) break;
total += (quantity * price);
}
while (true);
return 0;
}
I'm attempting to make a program that asks for a class size that will define how many times the program asks for a test score.
Once it gets this it asks for the test score using a for loop until it reaches the class size.
What I want to do is record each score so that it can be announced at the end but I'm not sure how to record each seperate input within the code I am using. I want it to run something like:
Enter Score: 95
Enter Score: 25
Original Scores: 95,25
if the user entered the class size of 2. How do I do this? Maybe with an array but I dont know how to encorporate this?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int size;
double score;
cout << "Enter class size <1-20> \n";
cin >> size;
for (int i = 0; 0 <= size; i++) {
cout << "Enter Score \n";
cin >> score;
}
return 0;
}
Just before the for loop, instantiate a std::vector<double> scores(size);. That is what you will use to record all the scores. (Write #include <vector> to bring in this functionality.)
Then fix the typos in your loop for (int i = 0; i < size; i++) {.
Then adjust the cin to cin >> scores[i];. (Isn't the C++ standard library clever?! Don't try to understand the mechanics behind that just yet - just accept the notation as plausible).
You can then iterate through that std::vector to output the scores. Lots of questions on this site to show you how to do that.
(Eventually you'll use a std::size_t as the type for the vector index rather than an int.)
You can do it just using a double array. Here is the code snippet.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int size;
double score[20]; // as size must me less than or equal 20
cout << "Enter class size <1-20> \n";
cin >> size;
for (int i = 0; i < size; i++) {
cout << "Enter Score \n";
cin >> score[i];
}
cout<<"Original Scores: ";
// Now iterate through all scores.
for(int i=0;i<size; i++){
if(i){
cout<<",";
}
cout<<score[i]
}
return 0;
}
Note: score size (20) is not checked.
It's possible to write even more compact code for reading values using range-for:
size_t sz;
std::cin >> sz;
std::vector<double> input(sz);
for(auto& el : input)
std::cin >> el;
First off, this is for a homework assignment, so I'd appreciate help and guidance rather than just the answer in code.
The purpose of the code should be for a user to input a number and a width.
If the width is longer than the number, the number will be printed out with zeros in front of the number. For example 43 3 would give 043.
If the width isn't longer just the number would be printed: 433 2 would be 433.
I think I have to get the count of characters in the number and compare it to the count of characters in the width (if-else statement).
Then, if the number of characters in the number is more, print out the number. Else, print out the width.
I think I get the number of zeros by subtracting the length of the number from the length of the width. Then use that to set the number of zeros. Like I said this is homework and would rather learn than be given the answer.
If anyone can help, it'll be appreciated.
#include <iostream>;
#include <string>;
using namespace std;
string format(int number, int width) {
int count = 0;
if (number > width)// This if-else is incomplete
return ;
else
}
int main()
{
cout << "Enter a number: ";
string n;
cin >> n;
cout << "Enter the number's width: ";
string w;
cin >> w;
format(n, w);
}
no need to checking string or other things write these code C++ will do it for you automatically.
#include <conio.h>
#include <iostream>
using std::cout;
using std::cin;
#include <string>;
using std::string;
#include <iomanip>
using std::setw;
void format(int number, int width)
{
cout.fill('0');
cout << setw(width) << number;
}
int main()
{
cout << "Enter a number: ";
int n;
cin >> n;
cout << "Enter the number's width: ";
int w;
cin >> w;
format(n, w);
_getch();
return 0;
}