C++ :: Entering Valid User Input?

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


ADVERTISEMENT

C++ :: Entering Valid User Input?

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

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 View Related

C :: Way For User To Stop Entering Values

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

Visual C++ :: Showing Time For A User While Entering Some Characters

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

C++ :: Write Program Where User Will Keep Entering Data Until Pressing Ctrl D

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

C++ :: User Array Size And Sorting User Input

Nov 1, 2014

I am making program that allows the user to determine how big the array size will be and then asks the user to make up numbers to fill the array. Every time run the program on Dev C++ it says "program has stopped working"

Heres My Code:

//Assignment 19 Program 2
#include <iostream>
using namespace std;
int main()

[Code].....

View 3 Replies View Related

C++ :: Error Entering A Function?

Jul 2, 2013

I have no error compiling, but running my project it stops before entering a function and debugging I have an error about Segmentation fault. The function:

Code:

Mat logGabor(matriz filter,Mat filter,double r_o,double theta_o,double sigma_theta,
matriz radius,matriz theta,int cols,int rows,double sigma_r,int *padSize){
Mat rpad;
int k=*padSize;
printf("Welcome to the function"); //Here the error
...

More info: I created the matriz as : typedef double** matriz;
and a createmat function that give back a double** and in this function I allocate space for matriz and works. In the main:

Code:
Mat chr-Rpad[4][5]; //chrOrient=4;chrScales=5
for(int i_or=1;i_or<chrOrient;i_or++){
for(int i_sc=1;i_sc<chrScales;i_sc++){
//some math calculation
chrRpad[i_or-1][i_sc-1]=abs(logGabor(filter,imftt2,r_o,theta_o,sigma_theta,radius,theta,cols,rows,sigma_r,padSize));

View 6 Replies View Related

C :: Entering Characters Into A String Pointer

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

C++ :: Entering Command Line Arguments?

Jan 11, 2015

I am not sure how to enter command line arguments when I run the executable of the file below. I want check an make sure that only two arguments get into the main() before running the rest of the code. I'm using bash on linux.

An example that I have tried to test for 2 arguments in command line -arg1 arg2 > ./a.out This of course does not work

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

[Code].....

View 4 Replies View Related

C :: Entering Two Positive Integers To Multiply Together - Program Won't Compile

Aug 24, 2013

I'm trying to get my C program to compile but it's not working at all. I've programmed a little in C++ before but I'm not used to C. Here's my program so far:

Code:
int main(void){
// Establishes variables
int num1, num2, product;
float quotient;

[Code] .....

It keeps giving me an error message as follows:

"/usr/bin/ldrelabwk2: file format not recognized; treating as linker script
/usr/bin/ldrelabwk2:1: syntax error
collect2: ld returned 1 exit status"

View 11 Replies View Related

C :: Inventory Program - Entering Amounts With Two Decimal Places

Jun 7, 2013

So my inventory program runs fine as far as I can tell. Except when entering "Amounts" with more that 1-precision . ie

Amount Entered : 25.99 // Problem
Amount Entered : 25.9 // No Problem

When entering say 25.99, the rest of the file is jibberish. but 25.9, the rest of the file is fine

Code:
#include <stdio.h>
typedef struct toolRecord{
int record_num;
char tool_name[16];
int quantity;
double price;

[Code] .....

View 5 Replies View Related

C++ :: Concatenation Of Strings When Entering City / State And Zip Code

Apr 15, 2013

I have written my code, but can't seem to figure out why it doesn't enter the while loop and start asking for Input.

Code:
#include <iostream>
#include <string>
using namespace std;
int main(){
//declare variables
string cityName = "";
string stateName = "";

[Code] ....

View 3 Replies View Related

C++ :: Program To Determine Slope Of A Line - Stuck On Commas When Entering Numbers

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

C++ :: Valid Function Prototypes?

Mar 11, 2013

I was asked by a friend about validity of following function prototypes,

void func1(int = 0, int*);
void func2(int = 1, int& = 2);
void func3(int*, int& = 3);
void func4(int& = 4, int* = 0);
void func5(int& = 0, int = 1);
void func6(int = 5, int& = 6, int* = 0);

I think the only prototype that is invalid is func1 because it does not have default parameter on the far right.

View 16 Replies View Related

C# :: Valid To Use And / Or When Using Conditional Operator

Jan 31, 2014

I want to know if it's valid to use and/or when using the conditional operator.

Example:

value = textBox1.Text;
decimal? qty = (value.Equals("0") || value.Equals("0.0")) ? null : (decimal?)decimal.Parse(value);

View 2 Replies View Related

C++ :: Ofstream Valid Path And Name

Jan 27, 2013

The following code writes to a file on either local disk to a remote disk (commented out code) on Windows 7 platform.

Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outfile;

[Code].....

The documentation does not specify what is a valid filename (path and filename). For example, will the "\server emp" path work on all operating systems to access a samba share? Does the constructor accept forward and backward slashes as folder separator on all operating systems?

View 1 Replies View Related

C :: Valid Email Address Check

Jul 13, 2013

Now I have to write a code which would determine whether an Email address is valid or not.

In my exercise a valid address should look like this : ___@___.___.il (___ for any letters)

E.g. Valid address:

something @ something . something . il Invalid: tami @ jce . ac . uk

(without spaces of course)

Code:
#include <iostream>
#include <string.h>
using namespace std;
int isValid (char s[]) {
int length=strlen(s), ind1=0, ind2=0;

[Code] ....

It doesn't work well. It says both addresses are wrong when the 1st one isn't.

View 9 Replies View Related

C :: Check If Certain 9x9 Sudoku Grid Is Valid?

Sep 30, 2013

So my program is to check if a certain 9x9 sudoku grid is valid. i have to get the input through command argument. so for example.

./a.out sudoku.txt

So we have make my c program to use FILE I/O open and what not

program behavior must be as follow File does not exist.File contains something other than a sequence of 81 integers (too many, too few, non-int).

One or more of the values is not in the range 1..9 Violation of Sudoku rules (this is the big one!) In case 4, you should report the violation (or any one of the violations if there are multiple -- you do not need to exhaustively enumerate all violations).

For example: Row Violation: entries (2,2) and (2,6) are both equal to 7. (Similarly for column and box violations). All i know is that i need to make a 2d 9 by 9 array

View 12 Replies View Related

C :: Are Musical Notes Valid In Program

Jul 21, 2013

Is this a good way of writing this program?I wanted to start fresh with my new code though and a better title.Basically, I took what I learned from my questions in that thread and managed to build a list of musical notes( octaves, frequencies, sharp symbols, basically everything ).

Code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX_NOTES 88 /* 88 keys on a standard piano */

[code]...

I already know the frequencies are correct( they are rounded, but they should be close enough to the value ), but I wasn't so sure about the note labels. Obviously though, I'm not done with this program and I will be adding most of the functions Anduril suggested to take a text file and convert it into music. I just wanted to make sure I had everything correct before moving on. I probably didn't do the GetNextOctave() and GetNextNote() functions very efficiently so need suggestions on those functions also.

View 3 Replies View Related

C++ :: How To Make Sure Dereference To Vector Is Valid

Oct 6, 2014

I have this piece of code in parts of my path finding algorithm

for( int head; head < q.size(); ++ head ){
walk& w = q[head];

// do manything with w
if( some_condition ) q.push_back( walk( w.x + 1, w.y, head ) );
}

However I notice that sometimes w is cannot be dereferenced. It can but it throws junk number at me. Perhaps the vector is changing it size and move the whole array to a different location. Is there anyway to make sure that w is always valid ?

I just want to use w because of shorter typing and cleaner look not because of performance. I also refrain from using macro.

View 8 Replies View Related

C/C++ :: RAM Does Not Give A Valid Preprocessing Token

May 2, 2014

#define PROJECT_ID ram  
#define QUOTES(FILENAME)  #FILENAME
#define DATA_VAR_FILENAME(PROJECT_ID) QUOTES(../##PROJECT_ID##_data_var.h)
#define DATA_VAR_FILE(PROJECT_ID) DATA_VAR_FILENAME(PROJECT_ID)
#define CUST_DATA_VAR_FILENAME DATA_VAR_FILE(PROJECT_ID)

When I tried to include CUST_DATA_VAR_FILENAME like below
#include CUST_DATA_VAR_FILENAME

Will get below error
error: pasting "/" and "ram" does not give a valid preprocessing token

View 1 Replies View Related

C++ :: Check If Given 8 Byte Is A Valid Double

May 28, 2014

Is there a function in C/C++ that can check if a given 8-byte data block is a valid double value in the valid range?

View 7 Replies View Related

C++ :: Second User Name Input Is Being Skipped

Jan 1, 2014

In this simple end-of-chapter problem, the ages of two people are to be compared and determine who's older. The problem I'm experiencing is that the second user name input is being skipped and going straight to the second users age.

So while running the program it looks something like:

What is user ones name?
<input name>
How old are they?
<input age>
What is user twos name?
How old are they?
<input age>
<if statement result>

Here's my code:

Code: #include <iostream>
#include <string>
using namespace std;
int user_one_age;
int user_two_age;
string user_one_name;
string user_two_name;

[code].....

View 9 Replies View Related

C# :: TexBox User Input To Hex Value

Sep 2, 2012

So I've got this form that a user puts in a numeric value into a text box. This value then has to be placed into a byte string of data so that it can be transmitted over as a packet. Bellow is what I have so far:

Code:
Byte[] OUTBuffer = new byte[65];//Allocate output memory buffer
Byte[] INBuffer = new byte[65]; //Allocate input memory buffer

if (setmSpeed == true)
{
OUTBuffer[0] = 0; //Not used, must be set to 0
OUTBuffer[1] = 0x85; //Command mode

[Code] ....

I've put in red the area where I'm having issues. I've tried different methods and have not been able to get this working yet. It only works if I speciffy the value manually as with OUTBuffer[1] = 0x85;, but I want byte 2 of OUTBuffer[] to be set with what I put in the text box.

View 4 Replies View Related

C :: User Input To File

Mar 18, 2013

I'm creating a program that should create a structure of a list of people entered by the user; the only problem I'm having is with the %c. When I have it, the loop only occurs once; if I put a %s, the loop occurs up to 25 times, but it doesn't write to the text file. Here is the code:

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
[code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved