C++ :: How To Restrict User To Enter Four Digit Number Only
Aug 22, 2014
I am trying to write up something to have a user to enter a four digit number. Only four digits, Ex: 0001, 0116, or 9999. There is no getting around the selection 0001 or 0002. I understand if not done correctly, the first three 0's will be ignored.
I've been just playing around with what I have below but I just don't remember how to do it nor can I find a good example online to figure it out. I know it is not correct just typing to try to remember. I am aware that it is gibberish right now, this is just me brainstorming.
int number;
cout<<("Please enter the four digit number(Ex: 0001):
");
cin>> setw(2) >> number;
cout<<)"Please enter four digit date. Two digits for month and two digits for year:
");
cin>> date;
if (number< || number > 30)
cout << "Invalid choice. Try again." << endl;
cin.clear();
Im trying to create a program that has the user input a 5 digit number. If it's between 10000 & 99999, it will do one thing..(just saying 'yes' for now. Outside those numbers will prompt the user to input again. However, if the user inputs the exact digits 76087, it should display 'term'.
This current code is displaying 'term' whenever the user inputs the 5 digits.
Code:
#include <iostream> using namespace std; int main() { int pin; cout << "Welcome to Movie Food Enter your 5-digit pin code: " ;
Write a C++ program that asks the user to enter an even number only. If the user enters an odd number the program should use input validation to ask the user to enter another number.
- The user can enter as many numbers as he wants
- The program should also keep a count of how many numbers entered and display how many odd and even numbers entered.
Write a program that prompts the user to enter an item#, the program should determine if the item is in the file and print the price of the corresponding item. If the item is not in the file an error message should be printed.
All I have so far is
string item_no=0; cout<<"Enter a item#"; getline(cin,item_no); if stream openData;
Write a function which will take 3 arguments. The function needs to return a new number which is formed by replacing the digit on a given position in the number with a digit which is carried as an argument (the position in the number is counted from right to left, starting with one). Write a main program which will print the newly formed number.
Examples: A function call of 2376, 3 and 5 should return the number 2576 A function call of 123456, 4 and 9 should return the number 129456
What I succeeded to do so far: Figure out the logic for swapping the digit and write working code for it (in the main function).
What I failed to do so far: Write a function which will return the desired result.
What is my problem: I tried writing a function to do this, but as you see from my calculations, my result is divided in 3 parts. I don't know how to return more variables from a function.
Code:
#include <stdio.h> int main() { int inputNumber, swapPosition, swapDigit; scanf("%d%d%d", &inputNumber, &swapPosition, &swapDigit); int i, numberPart1 = inputNumber; for (i = 1; i <= swapPosition; i++)
I'm trying to create a program that allows the user to enter an unknown number of income and expense amounts. The program has to us see a while loop and display the income total, expense total, and total profit or loss. I've got it really close, but I'm doing the loop wrong. It counts the sentinel value (-1) towards the total, instead of just ending like it is supposed to.
#include <iostream> using namespace std; int main() { //declare variables double incomeInput; double expenseInput; double incomeTotal=0; double expenseTotal=0;
I've seen some threads that include the information about what data types one has to use usually to put a number of 12digit. But honestly speaking none of them works for me & it takes me a lot suffering about the data type. Even I've googled but I didn't get any specific result.
So, which data type I'd have to use to enter 12digit number.
I'm writing a code to find the largest prime factor of the number 600851475143
Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.
Sample Output 1:
8-bit Binary Number => 11111111 Decimal Number = 255
Sample Output 2:
8-bit Binary Number => 10101010 Decimal Number = 170
Sample Output 3:
8-bit Binary Number => 101010102 Number entered is not a binary number
#include <iostream> using namespace std; int main() { int num;
Return the digit at the user specified index of an integer. If the integer has n digits and the index is NOT in the range 0 <=index <n return -1 Start the digit numbering at 0. Example, if user input is 4 (index) and the integer equals 123456790 the return value for the function is 5 (start index at 0) ; if user input is 40 (index) and the integer equals 123456790 the return value for the function is -1
#include <iostream> #include <istream> #include <cstdlib> #include <cassert> #include <string> using namespace std; int getIndex(int, int);
I'm having trouble converting a 4 digit number into a BCD number, in the program I did below I was able to convert a 2 digit number into BCD, but I do not know how to convert a 4 digit number or how to start it.
#include <cstdlib> #include <iostream> #include <string> using namespace std; int main(int argc, char *argv[])
I wanted to find all the prime until a specified limit in C. I use the Sieve of Eratosthenes. But when I define the limit to anything more than a 7 digit number the program terminates.
Code:
#include<stdio.h> #define limit 1000000000 int main(void) { unsigned long long int i,j; int primes[limit] = {0}; //int count =0; for(i=2;i<limit;i++) }
[code]....
I believe that this might be because the size cannot be declared array cannot be more than the a 7 digit number. I think so. how to store a 10 digit number in C?And can't unsigned long long hold a 10 digit?
I am trying to remove the first digit so if the user enters 12345 it should output 2345 the code i have works only for removing the last digit how would i go about removing the first one?
#include <iostream> using namespace std; int removeFirst(int n); int main(){ int n, m; cout << "enter number" << endl;
So I have to ask the user to enter a positive 12 digit number, and it has to be 12 digits exactly. I thought I'd do
Code:
unsigned long long int x; do{...} while(!(x>99999999999 && x<1000000000000));
This would obviously be fine on my 64 bit machine, but the code will be ran on a 32 bit one, where unsigned long long is, if I'm not mistaken, 32 bits. Which has a max value of 4 billion and something.
How to get all the possible combinations for 4 digits from a 5 digit number. I need a pair that has both 5 digits and four digits. their sum must be equal to a five digit user input. i.e.
user input : 14690 output: 14690 has pairs 12345 + 2345 2345 came from 12345 lets say that x = 12345 and y =2345 besides y == x%10000
other formula can i have since if i use % and / i will have a lot of declarations....
Given an integer, find the sum of all the digits in the number until the sum becomes a single digit. E.g. sum of digits of 9264 = 21. Then sum of 21 = 3.
//using bloodshed dev c++ compiler //How can I output the name and a two digit number next to it //example would be Indiana Jones 20 #include<iostream> #include<string> using namespace std; class Customer {