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;
}
Related
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 2 years ago.
I want to block all the letters for input in the following code, can you help me with that?
#include <iostream>
using namespace std;
int main()
{
cout<<"To close this program you need to type in -1 for the first input"<<endl;
int m, n;
do{
int counter1 = 0;
int counter2 = 0;
cout<<"Now you need to input two seperate natural numbers, and after that it calculates the difference of both numbers factors!"<<endl;
cout<<"First Input"<<endl;
cin>>m;
if(m==-1){
break;
}
cout<<"Second Input"<<endl;
cin>>n;
if(m<0 or n<0){
cout<<"ERROR - Only natural numbers are allowed!"<<endl;
}
else{
...
The rest of the program is just the math.
When you declare a type of a variable, the variable can't contain anything else than what you have declared. So: You can’t use an int m to input a float. You could however use cin.ignore() (more details here) to accept a user input of "4.1" as "4". Here you go:
#include <iostream>
#include <limits>
using namespace std;
int main() {
cout << "Enter an int: ";
int m = 0;
while(!(cin >> m)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input!\nEnter an int: ";
}
cout << "You enterd: " << m << endl;
}
I am having some problems with my code, here is a part of the code I am having problem with.
Point of this code is that if a user enters something that is not a number, for example a character p, program should ask user to enter again, this part works.
If user enters a combination of numbers and characters program should ask user to enter again. For example n12 or 12n are not valid.
Part where a character comes first, like n12 doesn't cause problems, but the problem comes where a number is first and something else second, like 12n, which is not valid, but my code prints out the number 12 and later says that number is not valid.
#include <iostream>
using namespace std;
int main ()
{
int n;
while(1)
{
cout<<"Enter a number, 0 for exit: ";
cin>>n;
if(!cin)
{
cout<<"You didnt enter a valid number\n";
cin.clear();
cin.ignore(1000,'\n');
continue;
}
cout<<"Number is: "<<n<<endl;
if(n==0) return 0;
}
}
Examples of output of the code:
1°
Enter a number, 0 for exit: 12
Number is: 12
2°
Enter a number, 0 for exit: n
You didnt enter valid number
3°
Enter a number, 0 for exit: n12
You didnt enter valid number
4° <--- this one does not work properly
Enter a number, 0 for exit: 12n
Number is: 12
Enter a number, 0 for exit: You didnt enter valid number
Edit: if it's possible, I'd like to solve this problem without including additional libraries.
You could use isdigit(), std::string and std::all_of() from additional standard libraries (which is okay, not an overkill), like this (you store the input in the string and then you check if for every character of that string the isdigit function succeeds, which means that the input is purely numerically in that case):
#include <iostream>
#include <cctype>
#include <string>
int main()
{
std::string str;
std::cin >> str;
(std::all_of(str.begin(), str.end(), [] (char c) { return isdigit(c); })) ? std::cout << "It's a number!\n" : std::cout << "isdigit() failed\n";
return 0;
}
Output:
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
4
It's a number!
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
f
isdigit() failed
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
12n
isdigit() failed
You need to check the whole line that the user entered, not just part of it. istream::operator>> stops reading when it encounters a character that does not belong to the data type currently being read. That is why input like 12n gets processed as 12 and n separately.
You are not going to solve this using just <iostream> functionality alone. This is best handled using things like std::getline() and std::stoi()/std::strtol(), eg:
#include <iostream>
#include <string>
using namespace std;
bool to_int(const string &str, int &i)
{
size_t pos;
i = stoi(str, &pos);
return (str.c_str()[pos] == '\0');
}
int main ()
{
string line;
int n;
do
{
cout << "Enter a number, 0 for exit: ";
getline(cin, line);
if (!to_int(line, n))
{
cout << "You didn't enter a valid number\n";
continue;
}
cout << "Number is: " << n << endl;
if (n == 0) break;
}
while (1);
return 0;
}
If you don't want to use a conversion function like std::stoi(), at least use a std::istringstream instead:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool to_int(const string &str, int &i)
{
char c;
istringstream iss(str);
iss >> i;
return !(iss.fail() || iss.get(c));
}
int main ()
{
string line;
int n;
do
{
cout << "Enter a number, 0 for exit: ";
getline(cin, line);
if (!to_int(line, n))
{
cout << "You didn't enter a valid number\n";
continue;
}
cout << "Number is: " << n << endl;
if (n == 0) break;
}
while (1);
return 0;
}
Loop through the number, if it has something other than a digit, print "You didnt enter valid number", else print the number. We will be getting input as a string and using ctype.h to verify if the string's character it is a digit:
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string n;
bool is_valid = true;
while (1)
{
cout << "Enter a number, 0 for exit: ";
cin >> n;
for (size_t i = 0; i < n.length(); i++) {
if (!isdigit(n[i])) {
cout << "You didnt enter a valid number\n";
is_valid = false;
break;
}
}
if (is_valid) cout << "Number is: " << n << endl;
if (n == "0") return 0;
}
}
So I have a program here that is supposed to print to the screen permutations of a user input word that can be 4 to 10 characters long and there are supposed to be as many permutations as there are letters in the word. I almost have complete success, but there is one issue. When it prints the permutations, after the first about 2 permutations, it starts to not use all the letters and/or the same letter twice.
For example, if the user input word is "bill", the output is as follows:
llib illb ibll lbii
The fourth is is obviously not correct. The problem is more apparent with words that have more letters. I need it to only use the letters it has once. How do I go about fixing this in my code? Here is my code.
int main(void)
{
string word;
string randword;
string reverse;
int length;
int i = 0;
int j = 0;
string randCH;
cout << "Enter any word 4 to 10 letters in length: ";
cin >> word;
//Checks if word is less than 4 or greater than 10
while (1)
{
/*The code here is in the final program and I know it works. The problem is not here*/
}
length = word.length();
//Uses reverse function
reverse = reverseit(word);
/*reverseit is a function outside of main that makes the word backwards*/
//Prints all permutations
cout << endl << reverse << " ";
for (i = 0; i < word.length() - 1; i++)
{
for (j = 0; j < word.length(); j++)
{
randCH = word.substr(rand() % length, 1);
cout << randCH;
}
cout << " ";
cout << endl;
you can use std::next_permutation which is already built to achieve this:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string word;
cin >> word;
sort(word.begin(),word.end());
do {
cout << word <<endl;
} while(next_permutation(word.begin(),word.end()));
}
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;
}
I want to make a program in which user enter the three digits separated by the space and i want to show the smallest digit.
See my code:
#include<iostream>
using namespace std;
int main( )
{
int a,b,c;
cout<<" enter three numbers separated by space... "<<endl;
cin>>a>>b>>c;
int result=1;
while(a && b && c){
result++;
a--; b--; c--;
}
cout<<" minimum number is "<<result<<endl;
system("pause");
return 0;
}
Sample input:
3 7 1
Sample output:
2
It doesn't show the smallest digit. What's the problem in my code and how can I solve my problem?
The result should be initialized by zero
int result = 0;
nevertheless the approach is wrong because the user can enter negative values.
The program could be written the following way
#include <iostream>
#include <algorithm>
int main( )
{
std::cout << "Enter three numbers separated by spaces: ";
int a, b, c;
std::cin >> a >> b >> c;
std::cout << "The minimum number is " << std::min( { a, b, c } ) << std::endl;
return 0;
}
There's a hidden assumption here that a, b and c are positive. If you allow for such assumption, you're nearly there - you just need to initialize result to 0 instead of 1.
hint:
When writing c++ always prefer to write code in terms of the algorithms that have been thoughtfully provided for you in the standard library.
#include <iostream>
#include <algorithm>
using namespace std;
int main( )
{
int a,b,c;
cout<<" enter three numbers separated by space... "<<endl;
cin>>a>>b>>c;
int result = std::min(a, std::min(b, c));
cout<<" minimum number is " << result<<endl;
system("pause");
return 0;
}
Much pain, it will prevent. More productivity, it will produce.
;)