C++ :: Input Validation - Prevent User From Entering Characters Or Symbols Instead Of Numbers?
Jul 19, 2013
How do I prevent user from entering characters or symbols instead of numbers?
int num;
cout<<"Enter a number."<<endl;
cin>>num;
Also, how do I prevent user from entering a number instead of a character?
char c;
cout<<"Enter a character."<<endl;
cin>>c;
View 9 Replies
ADVERTISEMENT
Oct 23, 2014
I need validation on what the user inputs. Input should not be an alphabet, empty, and not negative number. This program is for finding the GCD and LCM of multiple numbers.
#include <stdio.h>
void bubble_sort(int numbers[], int len) {
int i, j;
int swapped;
[Code] .....
View 2 Replies
View Related
Dec 26, 2014
Im working on my project for college and i want to know how to add sth similar to a chronometer for user to see how much time have passed while entering some characters using cin.getline function.
View 2 Replies
View Related
Mar 19, 2013
How can I write my simple program so if the user enters an invalid number, The program won’t exit? I know I am supposed to use a if (cin) or if (!cin),
But I don’t know where in the program or how I should use it. Right now my Program looks kind of like this:
If (number > 1 && number < 1001)
Go through some function loops
Else
Cout << “invalid number”;
I need to write it so when the user enters an invalid number, the program would Keep asking for the right number until it's given.
View 1 Replies
View Related
Mar 20, 2013
Entering valid user input?
How can I write my simple program so if the user enters an invalid number, The program won’t exit? I know I am supposed to use a if (cin) or if (!cin), But I don’t know where in the program or how I should use it. Right now my Program looks kind of like this:
If (number > 1 && number < 1001)
Go through some function loops
Else
Cout << “invalid number”;
I need to write it so when the user enters an invalid number, the program Would Keep asking for the right number until it's given.
View 3 Replies
View Related
Aug 6, 2013
how to ask the user to input operating symbols like "+","-","*" and then use it to operate on the numbers. The user should be asked to input the symbols not like this
1.+
2.-
3.*
View 2 Replies
View Related
Feb 4, 2014
How I could correctly validate user input when the user inputs a numeric value that will be float pay1, pay2, pay3, pay4. However, the if statement that I wrote crashes after I test the validation. I been told that scanf is dangerous, but strtol works best, but how to write a validation with strtol.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define ARRAYSIZE 2
#define FORMAT "First Name Pago1 Pago2 Pago3 Pago4 Total Net"
#define FORMATHEADER "Last Name "
[Code] ....
View 3 Replies
View Related
Dec 20, 2014
I'm trying to validate my input. I require for the user to enter six doubles and if they don't then I want them to re-enter the information. Here is my code:
Code:
while (1>0) {
printf("Please enter arguments in the order: negative mass, positive mass, initial x-position, initial y-position, initial x-velocity, initial y-velocity:
");
if ( scanf("%lf %lf %lf %lf %lf %lf",&MassMinus,&MassPlus,&Pos[0][0],&Pos[0][1],&Vel[0][0],&Vel[0][1]) != 6) {
printf("Not all numbers were assigned!
[Code] .....
At the moment it just waits if you enter less than six numbers and if you enter any more than 6 it just ignores anything after the sixth number (so pretty much does nothing). Also if I entered 1 2 a b 3 4 instead of entering six numbers it would register that as 1 2 0 0 3 4 but I want it to make the user input the numbers again. I'm also aware that "while (1>0)" isn't good programming form but I'm not really sure what to use instead?
View 11 Replies
View Related
Nov 12, 2014
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
[Code] ....
This program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers.
View 1 Replies
View Related
Feb 8, 2013
1. The debugger shows that the characters are entered into the word pointer, and when a punctuation or space character is encountered, I terminate the string with a ''. But when I use puts to print the string, garbage is printed. Why is this?
2. Also, if I don't allocate memory for word the compiler gives a warning about it being used uninitialised. But I didn't allocate memory for the array of pointers(words), and the compiler didn't give any warnings. Whats the difference between a pointer and an array of pointers?
Code:
// wordcount.c - count words.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
[Code]....
View 7 Replies
View Related
Mar 13, 2013
how to read characters from user and construct a sting and then extract part of it using substring?
View 2 Replies
View Related
Jan 5, 2015
I have got to create a program that will gather input from the user and then encrypt & decrypt those characters.
I'm not very confident at coding so I'm sure many parts of my code are written poorly and not following the best practice so I have written a simple version of an algorithm where the program simply adds/subtracts a value of 2 to/from the ASCII values but I have discovered the use of the rand() and srand functions but I'm unsure how to go about using them within both my encrypt and decrypt function as a single value (static variable?).
Here is my code in its entirety at the moment.
#include <iostream >
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
char getMenuSelection (char m);
void encrypt (char key[]);
void decrypt (char code[]);
[code]....
This code currently doesn't execute due to ' undefined reference to 'encrypt(char*) '
Question
I am asking how to generate the random number and incorporate this into my encrypt and decrypt functions.
1. Would the use of a static or global variable make this work as is?
2. Would I need to create separate class files for both functions?
View 2 Replies
View Related
Nov 24, 2014
I have trouble printing out the movies starting with inputing only two letters.
#include <iostream> //cout, cin, endl,getline
#include <fstream> //ifstream
#include <cstdlib> // exit
using namespace std;
const int NUM_MOVIES = 116792;
struct Movie {
[Code] ....
View 1 Replies
View Related
May 1, 2014
How to do the problem below using loop?
Input numbers until the user types a 0, then output the product of the non 0 numbers: e
E.g., if the user types 2 4 6 0, the program should output 48
View 3 Replies
View Related
Mar 6, 2015
Code:
#include <stdio.h>
#include <stdlib.h>
/*
You are to write a program that will allow a user to enter infinite numbers (greater than zero)(One number at a time). You must have a way for the user to stop entering values.Once the user stops, you will immediately display the following:
The lowest number entered is:
The highest number entered is:
The number of values entered is:
The average of the numbers entered is:
*/
main() {
int userNumber=0, sum=0, count=0, highNum=0, lowNum=0, lastNum = 0;
double average;
printf("Enter a number greater than 0: (Enter -1 to stop)
");
scanf_s("%i", &userNumber);
[Code]....
My program outputs everything I need except the lowest number entered. I have been spending a considerable amount of time on this problem to no avail.
View 4 Replies
View Related
Mar 1, 2013
I'm trying to write a program where the user will keep entering data until pressing control + D. Then the it will display the mean, smallest number, and largest number.
Here are three samples of my program running.
Sample 1
Enter the price for stock 1: $ 10
Enter the price for stock 2: $ 1.555
Enter the price for stock 3: $ 3.648
[Code].....
As you can see in Sample 1, the program runs correctly. The largest number was 20 and the lowest number was 1.555. But in Sample 2, the program shows min as 15.500, where it should be showing 15.000 and Sample 3, the program shows min as 110.000 when it should be showing 55.564.
Here's the code.
#include <iostream>
#include <iomanip>
using namespace std;
[Code].....
View 5 Replies
View Related
Sep 17, 2013
I created a C program that extracts some information from computer and displays it on screen. It is completed today and is going to be applied to startup programs of domain machines. It runs at logon and takes somewhat 5-10 seconds to finish execution. The output is shown on console screen & user can either close the window by clicking on 'x' or pressing 'Ctrl+c' during execution. How do i prevent both of these events?
Basically the most time consuming processes are
Code: popen("systeminfo","r");
popen("wmic qfe get installedon","r");
1st command is getting OS name,version and hotfixes installed.
2nd command gives me the date of updates installed.
I then filter out data & print required information only.
View 3 Replies
View Related
Jan 1, 2013
The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.
I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)
I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here.
// program to determine slope of a line
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
using namespace std;
void myflush ( istream& in ) {
[Code] .....
View 2 Replies
View Related
Jan 26, 2014
Now i got here a program that asks the user to input 2 numbers if the first inputed number is smaller than the second then show all even numbers from the range of the 1st inputed number till the 2nd inputed number, but if the first inputed number is greater than the second then display all odd numbers from the range of the 1st number till the 2nd.
now my question is how do i invert the odd numbers?(on the 2nd condition)
Code:
#include<stdio.h>
int main(void) {
int i,a,b,x;
printf("Enter two numbers");
printf("
First number:");
[Code] ......
View 4 Replies
View Related
Jan 15, 2014
//Finding prime numbers
#include <iostream>
using namespace std;
[Code]....
/*The program currently prints all the prime numbers up to n (For example, if 7 is entered, it prints out: 1, 2, 3, 5, 7. What I want it to do, is print out the first 7 numbers; 1, 2, 3, 5, 7, 11, 13.
View 1 Replies
View Related
May 6, 2014
#include <iostream>
#include <string>
#include <limits> //for std: numeric limits
#include <algorithm>
//Function to get an integer from the user that is greater than or equal to zero.
int getPositiveIntFromUser(const std::string& prompt) {
int retVal = -1;
[Code] ....
The first part works, but it doesn't calculate the GCD or LCM at all, it just crashes!
View 2 Replies
View Related
Sep 11, 2014
I'm working on a program in C++ that is supposed to read in a file, store the content of the file into a 2D array, assign characters to each of the numbers in the array and store in a char array, and print both of these arrays. It's then supposed to go through the initial array and make sure that each number doesn't differ in value from it's neighboring numbers by more than 1, correct these errors by replacing these numbers with the value of the average of their neighbors, assign characters to this corrected array as it did before, and print both arrays.
The character assignments go as follows:
0=blank
1=.
2=,
3=_
4=!
5=+
6=*
7=#
8=$
9=&
I have the code written that opens the file and loads the array, but where to go from there. To me the obvious, although probably not best, way to do the assignments is to go through the array with a for loop and use a series of if statements to check for the value of the number at each index and assign the appropriate symbol.
Here is the code I have so far:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int size = 100, i, j;
prog >> size;
int **numArray = new int* [size];
[code].....
View 2 Replies
View Related
Jun 10, 2013
I need a function to check if the inputted string contains a colon. It has to be in an if loop as the condition.
View 1 Replies
View Related
Jul 26, 2014
I am trying to generate a random number between two numbers that the user gives me.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void rand_int(const int &min, const int &max, int &val);
[Code] .....
View 3 Replies
View Related
Jun 9, 2014
How do I error check if the user is inputting letters and not numbers? For example, if the user inputs "Lab.txt" I need to display an error message. If they input "Lab2part2.txt" then this is correct and what I want.
I've found a lot of information online on how to error check for numbers or a single letter (EX: 1,2,3, etc. or 'A' 'B' 'C') but nothing for actual WORDS or maybe I should refer to it as a string of characters?
Is there any way to do this? Because my program requires I ask the user to input the name of the file. But the way my code is currently set up is even when the user inputs the wrong file name it still opens the file. I want to prevent this from happening so my thought was to error check user input.
/*Program to determine company's weekly payroll*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void OpenTheFile() {
ifstream inputFile;
string filename;
char letter;
int number;
[Code] .....
View 1 Replies
View Related
Oct 7, 2014
I tried to work on this program for while, but I am not able to get it to work
Directions:Write a program that reads an integer n from the user.
Display the first 100 numbers, with newlines every n numbers, using % operator
#include <stdio.h>
int main(void){
int a = 0;
int b = 99;
printf("Enter a number: ");
scanf ("%d", &a);
[Code] ....
When I run this code it asks me to enter a number. It doesn't matter what number I enter it gives me the numbers from 1 to 100 in one horizontal line.
I want this program to do something like this:
for example if the user enters n=6 the program should give:
0 1 2 3 4 5 6
7 8 9 10 11 12 13
and so on
View 2 Replies
View Related