C/C++ :: Data Type Checking With Pointers?
Sep 4, 2014
Assume the user has already put in the number of students (hence my variables numStuds, which will most likely be irrelevant to my problem).
So suppose I have this:
void inputStudentInfo(string *names, int *movies, const int numStuds) {
for(int i =0; i < numStuds; i++) {
cout << "Enter student name: "; getline(cin, names[i]); read_string(names[i]);
[Code] ....
Then I have my data type checking function:
//Data-Type Checking for strings
string read_string(string Sname) {
while(!cin.good())
[Code] ....
I am getting errors. I know the problem I think is that I am trying to data type check for a string made up of pointers* with just a string but I don't know how I am supposed to check this?
View 2 Replies
ADVERTISEMENT
Jan 22, 2014
Ok so I have this simple program that gets input from a user. I just want to put in a line of code to make sure that hte user can't type in something like "pizza" , I want to make it say that if the user puts in something that is NOT a number they will get a error back saying "Wrong! try again!" Here is my code :
#include <iostream>
using namespace std;
//Summation Program
//Function Prototypes
int get_num();
void compute_sum(int num, int &sum);
[Code] ....
View 6 Replies
View Related
Oct 13, 2013
Assuming I have a list of pointers to a generic type T:
#include <vector>
//...
list<T*> myList;
Now assuming I want to go on the list, and if T's type is matched to the type I'm looking for, then cast it to this type and do something. List shown here:
list<T*>:: const_iterator iter= myList.begin();
for(; iter!=myList.end(); ++iter){
if( typeid(*iter)==typeid(Something*)) //RUN-TIME ERROR
dynamic_cast<Something*>(*iter)->DoSomething();
}
how do I fix this run-time error?
View 1 Replies
View Related
Mar 23, 2014
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_READ 2
int main( ) {
char * secret = "abecedarium consisting This is a string literal!";
[Code] ....
I am trying to test what happens in the sscanf converted types (command and string) if they are smaller, i got surprised that the printf worked, so i dont know why.
this code is just a test for what i want to do, what i really want to do is for example: i have this string from the stdin which must have the following format:
connect name.surname
So I need to check the cases when it is not the input that i want, namely if the name.surname is for example bigger than 30 chars. and also if it has the abecedary characters from the ascii...
View 2 Replies
View Related
Jun 17, 2014
deal with a failed input stream and type checking input?
I use:
cout << "
Enter a menu option: ";
while(!(cin >> menuGameMode))
{
cin.clear();
while (cin.get() != '
')
continue;
cout << "Enter a menu option: ";
}
Is this the "Best" way to do it?
EDIT: We're assuming input is expecting an int.
View 9 Replies
View Related
Sep 6, 2013
I want to give a try to "restrict" type-qualifier. I have looked for examples on google and I see the format is used: "int * restrict p";
However when I write this I get an error: "p is undeclared"
I am using GNU GCC Compiler with Code::Blocks.
How can I solve this problem. Is it possible me to adapt the compiler to C99 standarts, if I can, will it solve my problem?
View 13 Replies
View Related
Mar 5, 2013
I think I need to use
Code:
while(input[0]!='
'){
scanf("%s", input)
printf("%s", pointerhere);
}
basically we were asked to make a program that will print everything before the user hits the enter button. we cannot use fgets and we have to use pointers here.
not sure what exactly to do. We were told not to use fgets cause it's so easy and we need to incorporate pointers in this project..
View 8 Replies
View Related
Jan 29, 2015
I always have confusions while using pointers with functions both as arguments and as return type.
View 1 Replies
View Related
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
Nov 6, 2013
I want to do simple program to capture some 5 surnames of people(employees) - using array and pointers. Well I understand a bit of arrays and I know how to loop through.
How can achieve this? This holds the key to my understanding how pointers work and can be used with char or strings.
View 2 Replies
View Related
May 7, 2013
I am looking for an example of when passing arrays to other data that has pointers,
I understand the terms I would just like to see a small example of code that actually demonstrates this process.
View 4 Replies
View Related
Feb 7, 2013
How can I move data from one file to the another file without using pointers.
View 7 Replies
View Related
Sep 15, 2014
unsigned short foo = 1337;
//since unsigned short is 2 bytes, a char x[2]should be enough to hold the binary data
char foo2[2];
How do I have foo2, contain the binary equivalent of foo? (how can I point foo2 to = &foo)
View 4 Replies
View Related
Mar 7, 2014
how to correctly use pointers within relation to function parameters and main source file.
I noticed that char types, for example char myVariable[50]; which is an array, does not seem to require a pointer as if it already has one built in? as opposed to char *myVariable; which seems to need one - i assume this is because char has different ways to store memory in relation to pointers, because of there being multiple ways to store a string, and memory allocation as a part of that. - i stared C a few weeks ago and feel that it is difficult to progress without nailing down pointers. Also address operators provide confusion for me and written tutorials are not so clear because there are different ways to use these operators.
View 4 Replies
View Related
Apr 23, 2013
I have the following method to read in a data file using a struct and a doubly linked list.
The readFile method is as follows:
Code:
struct contact *readFile(char * FName,struct contact **ptrList) {
struct contact *head, *newContact;
FILE *fptr;
char oneLine[60];
char *sname, *fname, *phone,*company, *email;
[Code] .....
My struct definition is like this:
Code:
struct contact {
char sname[15];
char fname[15];
char phone[15];
char company[15];
char email[15];
struct contact *prev;
struct contact *next;
};
View 2 Replies
View Related
Feb 11, 2013
Code:
typedef struct {
name_t name;
float beta[beta_num];
} gene_t;
[Code] ....
Is gene an array of address ? How come the compare function doesn't work ?
View 1 Replies
View Related
Sep 26, 2014
typedef struct example_dt_struct {
int a;
float b;
char* c;
};
typedef void(*func)(example_dt_struct *s, int e, int f);
void f(func *n){}
how can i use example_dt_structure with my function pointer into f()
View 5 Replies
View Related
Jan 21, 2015
I want to create a new data type called an inf_t. It's basically infinity (which for C++ is 1.7e+308). The only reason I want this is because I want to overload the cout << operation to print out INF/inf. Should I do this in a struct?
Code: struct inf_t {
private:
double inf = 1.7e+308;
};
std::ostream& operator << (std::ostream &stream, inf_t inf) {
stream << "INF";
return stream;
}
View 4 Replies
View Related
Mar 4, 2014
I want to find the size of the data type with out using sizeof() operator.
View 9 Replies
View Related
May 18, 2014
If I am asked to declare a data type for Date which should be in the format DD/MM/YY, which data type should i use for it? Is there any data type known as Date in C?
View 2 Replies
View Related
Apr 9, 2015
Is there a way to define a 3 bit or 12 bit data type in C/C++. I need these for defining my own packet having different bit length fields.
View 1 Replies
View Related
Feb 20, 2014
I am trying to use smart pointers to sort and re-link potentially large data elements. I have defined a class in my code for smart pointers, as listed below:
template <typename T>
class sptr {
public:
sptr(T *_ptr=NULL) { ptr = _ptr; }
[Code] ....
The object I'm trying to sort is a singly-linked list containing class objects with personal records (i.e., names, phone numbers, etc.). The singly-linked list class has an iterator class within it, as listed below.
template <class T>
class slist {
private:
struct node {
node() { data=T(); next=NULL; }
[Code] .....
The following is my function within my list class for "sorting" using the smart pointers.
template <typename T>
void slist<T>::sort(){
vector< sptr<node> > Ap(N); // set up smart point array for list
//slist<T>::iterator iter = begin();
node *ptrtemp = head->next;
[Code] .....
I must have a bad smart pointer assignment somewhere because this code compiles, but when I run it, std::bad_alloc happens along with a core dump. Where am I leaking memory?
View 6 Replies
View Related
Jun 12, 2013
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.
View 15 Replies
View Related
Sep 3, 2013
I know that an int is usually 4 bytes, ranging from -2^31 to 2^31-1 for a signed int and 0 to 2^32-1 for an unsigned int. My question is simply, bit-wise (I know they are labelled in the code), how does it determine whether to show -2^31 or 2^32-1 if it was 11111111 11111111 11111111 11111111 in bits? Is there a 5th byte to tell the compiler what data type to treat the input as?
View 3 Replies
View Related
Jan 4, 2013
Working on a Project Euler problem and the question asks for the largest prime number that is a factor of 600851475143. As you can see, this is significantly larger than the maximum of a long data type, which maxes out at 2147483647.
I'm running on Windows 32, so int64 is not a valid option for me. It seems like I'll likely have to use a different language to solve this problem.
View 4 Replies
View Related
Oct 15, 2013
I'm currently trying to solve a programming assignment and i got the logic of it, however i find it hard to implement.
What i need to do basically is fill an array with objects. Each object is a class that contains only one type of data. This means i can place int, double and string for example in one simple array.
However i can't figure out how to read data and then decide what it is. Even if i use templates once i call the function i have to give it a type, so getType<int> for example will not work with double or string.
I know about typeID and how to use it, i just can't figure out where to use it.
View 2 Replies
View Related