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;
}
Related
How can I have an output of
Sample Input No.1:
9
Sample Output No.1:
1.2.3.4.5.6.7.8.9
If you input numbers less than or equal to 9, the output should be (1.2.3.4.5.6.7.8.9)
And if you input numbers greater than 9, for example:
Sample Input No.2:
20
Sample Output No.2:
01.02.03.04.05.06.07.08.09.10
11.12.13.14.15.16.17.18.19.20
My code below is for Sample Input & Output No.2. I tried adding another for loop for SAMPLE NO.1 but it still reads Sample No.2 code. What should I do?
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int a, num;
cin >> num;
if (num > 100 || num <= 1){
cout << "OUT OF RANGE";
}
else {
for (int a = 1; a < num; a++){
cout << setfill('0') << setw(2) << a << ".";
}
cout << num;
}
}
kind of new to programming, don't know much🥲
As a possible solution, you could read the input as a string, then convert it to an integer.
Use the string length as the field width for the setw manipulator.
This should be able to handle values of (theoretically) arbitrary length.
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.
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;
Hello I am attempting to write a function that will generate a random string of lowercase letters. The length of the random string will be the number the user entered. I have this much so far but i believe i am over complicating things
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
char randString(){
int number;
str::string Str; //str has not been declared error
for(unsigned int i = 0; i <8; i++){
Str += randString(); //str was not declared in this scope error
}
cout << Str << endl; // str was not declared in this scope error
}
int main() {
char c;
int number;
srand (time(0));
cout << "Enter a number.\n"
"That number will generate a random string of lower case letters the length of the number" << endl;
cin >> number;
for (int i=0; i < number; i++){
number = rand() % 26;
c = 'a' + number;
cout << randString();
}
return 0;
}
You changed the value of the variable number inside your for loop, which is also your loop condition variable. This will cause an undefined behavior since the value of number will be changed every time the statement number = rand() % 26; is executed. However, from what I understood reading your problem statement, I think this is what you are trying to achieve:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
char c;
srand(time(NULL));
cout << "Enter a number.\n"
"That number will generate a random string of lower case letters the length of the number" << endl;
cin >> number;
for(int i=0;i<number;i++)
{
c = 'a' + rand()%26;
cout << c;
}
return 0;
}
Hope this helps. Good luck!
I have to make a program that asks for the number of rows and number of columns and prints a rectangle based on these values.
The only clue I got was:
Note that char tkn can be used to declare a character.
And I received an example of how input and output should look:
The number of lines: 3
The number of columns: 4
Which characters do you want to use: #
####
####
####
This is what I got so far (I'm just guessing how to do the char bit at the moment):
#include <iostream>
using namespace std;
int main()
{
int lines, columns, character;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
cin.ignore();
getchar();
return 0;
}
Add header
#include <iomanip>
and include the following loop
std::cout << std::setfill( tkn );
while ( lines-- )
{
std::cout << std::setw( columns + 1 ) << '\n';
}
Also it would be better to use identifier character (or simply c) or filler instead of this strange identifier tkn
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int lines, columns, character, i=0, l=0;
char tkn;
cout<<"The number of lines: ";
cin>>lines;
cout<<"The number of columns ";
cin>>columns;
cout<<"What character do you want to use? ";
cin>>tkn;
for(l=0;l<lines;l++;)
{
{
cout<<tkn;
}
for(i=0;i<columns;i++)
{
cout<<tkn;
}
cout<<endl;
}
cin.ignore();
getchar();
return 0;
}