C++ :: How To Create Type Validation Function

May 21, 2014

So I am writing an assignment to Detect prime numbers and it works by Asking for how many values you are going to enter, and then saying "Enter value 1: "

Then you would input and it would calculate via for loop, and that part is working. However to make my program more foolproof, I devised a way for the user to be unable to "Break" the program by inputting characters or float values. Here is the code for that:

while(!(cin >> num)){ //num is some type (char, float, int etc.)
cout << "That is not at valid input, please try again" << endl; //"Error Message"
cin.clear();
cin.ignore(10000, '
'); //Clear and reset cin
cout << "Enter value " << n << ": "; //Re-Prompt User for input
//n is whatever value the for loop is on
}

and this code works fine, I was just curious about how i would turn it into a function. Preferably wiht the name: ValidateInput(Param1, Param2);

The Parameters of the function preferably would be the variable youre inputting and the message you want to prompt. So somehow i wish to have it so for the above example it would look like:

ValidateInput(num, "Enter Value " << n << ": ");

But I don't know exactly how to label either parameter part because I want it to work for chars, ints, floats etc. And I don't know what I want it to return if anything either.

View 1 Replies


ADVERTISEMENT

C++ :: Credit Card Validation - Type Selection

Nov 17, 2014

We have to ask the user to select either a Visa card type or Mastercard type. Then, we must ask the user to enter the 16 digit card number and determine if the card is valid or not using module 10. If the answer is zero then it is a valid Visa card and if the answer is zero the answer is a valid MasterCard. My problem is that the I have not separated the validation for MasterCard separate from visa.

Here is what I have so far:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {

string type, num;

[Code] ...

View 5 Replies View Related

C# :: Trying To Create Custom Validation Attribute

May 8, 2014

I am trying to create a custom ValidationAttribute in my c# .net code. I have done a lot of searching and have code that should be working but the IsValid method is not firing as far as I can tell.

Model code:

[Required(AllowEmptyStrings = false, ErrorMessage = "required")]
//[Range(2014, 2100, ErrorMessage="Please enter a valid year.")]
//[RegularExpression(@"^d{4}$", ErrorMessage = "Please enter a valid year.")]
[ValidYear(ErrorMessage="Please enter one >= 2014")]
[Display(Name = "Exp. Year")]
public string expYear { get; set; }

And this is the class in that same model:

public class ValidYearAttribute : ValidationAttribute {
protected override ValidationResult IsValid(object value, ValidationContext validationContext){
return new ValidationResult("Something went wrong");
}
}

Why the IsValid is not firing.

View 3 Replies View Related

C++ :: Create Main Function With One Dimension Dynamic Array With Float Data Type

Dec 4, 2013

I need to create a main function with a one dimension dynamic array with float data type. The total number of array elements must be controlled by a user input from the keyboard. Test data is three different lengths 3,6,9 of the array. The lengths have to be set up at run time from users input. I understand how to create dynamic array but not where the user inputs the length of the array. How would I implement this?

View 6 Replies View Related

C++ ::  How To Create Umbrella Type

Nov 20, 2014

Any way to create an "umbrella" type/class/struct of existing c++ types or windows types.

I want to address ~3 different structs with a single pointer and downcast but, they are not a part of the same hierarchy.

Is there a way that I could for example's sake, include a POINT and a RECT under some "umbrella" type so I can go like this:

// Working Variables
//---------------------------------------------------------
Umbrella* pUmbType1(NULL);
Umbrella* pUmbType2(NULL);
POINT ptExamp = { 0 };
RECT egRect = { 0, 0, 0, 0 };

// Typecast POINT and RECT to umbrella pointer
//---------------------------------------------------------
pUmbType1 = (Umbrella*)ptExamp;
pUmbType2 = (Umbrella*)egRect;

I want to do this, so I don't have to duplicate code for each type.

View 4 Replies View Related

C :: Create Function To Create A Dynamic Array That Fill With Randomly Generated Integers From 0 To 50

Oct 26, 2013

I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.

Code:

* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}

[code]....

View 7 Replies View Related

C++ :: Create New Data Type To Store Big Integer?

Oct 17, 2014

i need to create a new integer data type called BigInt to store a big big integer, which includes Dint(8 bytes) and Qint(16 bytes)

here is the hint/

typedef struct BigInt {
Int data[2];
}

How can i "scanf" and "printf" them????

void ScanBigInt(const char *format, BigInt &x)
if format is “%dd” -> input Dint, if format is “%qd”--> input Qint
void PrintBigInt(const char *format, BigInt x)

View 3 Replies View Related

C/C++ :: How To Create Objects Of Same Data Structure (type)

Dec 31, 2014

well i create a State.h class

#ifndef STATE.H
#define STATE_H
class State {
public:
virtual void handle_action() = 0;
virtual void update() = 0;
virtual void render() = 0;
};
#endif //STATE.H

What i'm trying to create is a simple State Manager for SFML! I created another class that inherits State.

#pragma once
#include "state.h"
class FirstState : public State {
public:
FirstState();
~FirstState();
void handle_action();
void update();
void render();
};

So the question is this, each state that i have will inherit the State class. However, i wanted to perhaps add each state object into a vector array. But i'm not sure as to what data type it be? I have a state manager class that will contain the vector.

What i want to do is this, each game state will create an object that will inherit functions from the state.h class. I want to store them all in a vector array, but each object is clearly named different. My curiosity was wondering, since all those different states inherit the State.h class, can i simply create a State Object std::vector<State> *states; that will contain all those different state objects?

[URL]....

View 1 Replies View Related

C++ :: Create 1 Variable That Accept Any Type Of Values?

Aug 24, 2013

Can I create 1 variable that accept any type? And can I give it the NULL value too?

View 14 Replies View Related

C++ :: Create A Class Type Structure Using Struct Instead Of Classes

Apr 16, 2013

I am trying to create a class type structure using struct instead of classes.

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct myclass {
int * array;
int nelements;

[Code] ....

Guess what I am asking is using the pointer in the first code section better or is there another way'. I don't know about making the second code work.

View 3 Replies View Related

C++ :: Syntax Error In Function Call - Type Mismatch In Parameter In Function Sort

Jul 6, 2014

error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)

Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();

[Code] .....

View 11 Replies View Related

C :: Create New Data Type Data Declared As Int

Feb 1, 2015

I'm trying to create a new data type Data declared as int. What comes up to my mind is to create a structure like

Code:

typedef struct Data {
int number;
} Data;

but when I need to use the data I need to go through the structure. Is this a good way to declare a new data type?

View 2 Replies View Related

C++ :: How To Use The Value Returned From Int Type Function

Aug 28, 2014

I have this int type function that returns a number. It returns the value 2 for now but later it will return more variety of values. How do I use the value it returned? I'm not sure of the proper syntax.

View 5 Replies View Related

C++ :: Function Non-class Type?

Jan 10, 2015

My g++ compiler is telling me that said printTest() is not part of the class. I have presented it in header class and implemented it in .cpp class but still keep getting the error. Compile and compile error -

blackjack > g++ -Wextra -pedantic -std=c++11 Deck.h Deck.cpp test.cpp ;
test.cpp: In function ‘int main()’:
test.cpp:6:6: error: request for member ‘printTest’ in ‘test’, which is of non-class type ‘Deck()’
test.printTest();

My Code -

//Deck.h
#include <iostream>
#include <string>
class Deck{
public:
Deck();
void printTest();

[code].....

View 3 Replies View Related

C++ :: Get Return Type Of Function

Nov 21, 2012

Say I have overloaded functions with different arguments AND return types. Each set of argument has only one corresponding return type.

Code:
Vector grad(Scalar)
Tensor grad(Vector)

Later I have:

Code:
template <class T>
void test(T x) {
... Y = grad(x)
}

Then how do I automatically declare the type of Y. Do I need to make the grad function a template and specialize each of them ?

View 4 Replies View Related

C++ :: Type Signature For Function Prototype

Mar 30, 2013

The function im having problems with takes an array where each element is an array of unsigned chars i.e. octals representing a bitmap of one of 95 ASCII code characters and searches through this two dimensional array looking for a match for a predetermined of array of unsigned chars i.e. the bitmap of a predetermined char. If we find the char the function outputs the index in the two-dimensional array where each elem. is an array of octals ELSE it return -1 i.e. when the char is not found.

I have 2 files, one .cpp, the other .h. There is a function named find_char. See INPUT and OUTPUT on line 48 in .cpp file.

The exception im getting is: font2.cpp:23:45:error: invalid conversion from unsigned char to unsigned char(*)[5]

The input type specified for my function prototype corresponding to find_char. If I put just unsigned char it doesn't fix the problem because it's an array parameter i.e. like a call by reference. I've lead myself to believe that the array variable contains a pointer to the first value in the array and so I've made function prototypes that work with a T* i.e. a pointer to type T. Making the function prototype argument unsigned char* i.e. a pointer to unsigned char simply gives me the exception: "invalid conversion from unsigned char to unsigned char*". When I have the argument be 'unsigned char' I get undefined reference to find_char(unsigned char). URL.....

View 1 Replies View Related

C++ :: Switching Function Prototype To Another Type

Mar 29, 2014

This is my code:

#include<iostream>
#include<string>
using namespace std;
int bin2dec(const string binarystring); // Declaration of bin2dec prototype with one string parameter
int main() {
cout << "Enter Q to terminate program.

[Code] .....

Right now the code is in the form of a 'const string binarystring' and were supposed to be able to use 'const char binarystring[]' and for some reason whenever i try to switch it i run into problems when referencing the main function to the int bin2dec(...) function.

I wanted to know if theres a simple way to switch the prototype to an array type of function with [] without changing the entire code.

View 7 Replies View Related

C/C++ :: What Is The Default Return Type Of A Function

Jan 17, 2013

i heard that it was int but later it is deprecated , so at present what is the default return type of a function ?

View 4 Replies View Related

C/C++ :: Too Many Type Declaration Shown For A Function

Nov 3, 2012

It shows too many type declaration in the void push function. And I use turbo c++..

#include<stdio.h>
#define size 5
struct tack {
int top;
int item [10];
} void push(struct tack *s, int data)

[Code] .....

View 1 Replies View Related

C++ :: Setting Function Return Type

May 28, 2012

I want my function to return the type mpz_t, but I'm not sure how?

I've tried: mpz_t MyFunction(mpz_t A, mpz_t B){}

But it didn't work, here is my code so far, I have bolded the parts of the code which are causing errors and added the errors in the comments:

Code:
#include <iostream>
#include <mpir.h>
using namespace std;
???? A(mpz_t m, mpz_t n){
mpz_t mmo; //used to store the value of m, minus one

[Code] .....

Is there a way around this?

View 5 Replies View Related

C++ :: Function Pointer Typedef With Same Type Arguments

Mar 6, 2015

I need to create the following brain damaging abomination:

I need a function pointer type to a function that has an argument of the same function pointer type and returns the same function pointer type.

The purpose is to enable a type of subroutine threading scheme for a small application specific scripting language. The question could just as well have been posted to the C forum.

This syntax works, but Payload is a generic type which I can coerce into the right pointer type via a cast. This is ugly IMHO. I could also hide it as a pointer in the FlipState class since I've forward declared this.

But this is an extra indirection in a performance critical part of the code, and also ugly.

Code:
class FlipState ;
typedef PayLoad (*FuncPtr) (FlipState *fs, PayLoad P) ; This syntax blows chunks using gcc on the other hand. Code: class FlipState ;
typedef FuncPtr (*FuncPtr) (FlipState *fs, FuncPtr P) ;

[Code] .....

This is hardly surprising. The compiler could not possibly understand what I was defining in the typedef. I think what I need is some kind of way to forward declare a function pointer type and then redefine it properly.

Is such a think even possible or am I just SOL? This one is mind boggling. We know how to do this with classes or other complex data types, but the syntax eludes me for both C++ and C.

View 4 Replies View Related

C :: Variably Modified Type - Function Prototype

Mar 24, 2013

Variably modified types are subject to certain restrictions , just as variable-length arrays are. The most important restriction is that the declaration of a variably modified type must be inside the body of a function or in a function prototype.

Code:
void f(int m , int n) {
int a[m][n] , (*p)[n];
p=a;
//....
}

What does it mean? That we can't declare such a pointer as a global variable?

View 4 Replies View Related

C++ :: Returning A Structure As Function Return Type

Apr 4, 2013

I am having problems with my function definition of a function that should return a structure value.

This is the error I get compute.cpp(9): error C2146: syntax error : missing ';' before identifier 's_advertisebus'

The error is on the line where I start my function definition typing my function type as a structure. A long time ago in c the keyword struct is used with the structure type like struct s_advertisebus s_readadbus(). I tried it both ways but I got errors.

// struct.h
#ifndef STRUCT_H
#define STRUCT_H

struct s_advertisebus {
int nnumberofads;
float fpercentused;

[Code] ....

View 2 Replies View Related

C++ :: Using Pointers With Function Both As Arguments And Return Type

Jan 29, 2015

I always have confusions while using pointers with functions both as arguments and as return type.

View 1 Replies View Related

C++ :: Multi Condition Function For Char Type

Jan 30, 2013

I have always written like a>='0'&&a<='9'&&a>='a'&&a<='z' in loops etc, but no more. Basically add whatever you want to condition, and if you want point a to point b just separate them with a '-' sign. Simply

while(!isCharInside(x,"|a-zA-Z0-9.*/=|-|")){/*code*/}
bool isCharInside(char check,string condition="a-zA-Z*/+=|-|0-9"){
for(unsigned int x=0,z=getLenght(condition);x<z;++x){
if(condition[x+1]=='-'&&(condition[x]!='|'&&condition[x+2]!='|')){
if(condition[x]>condition[x+2])swap(condition[x],condition[x+2]);
for(;condition[x]<=condition[x+2];++condition[x]){if(condition[x]==check)return true;};x+=2;}
else if(check==condition[x])return true;
}
return false;}

View 1 Replies View Related

C++ :: Vector Whose Elements Have Function Pointer Type

Mar 30, 2013

"Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type."

View 9 Replies View Related







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