C :: Checking Validity Of Data From Stdin
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
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
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
View Related
Sep 19, 2013
I'm building a cross-platform library which links to some other 3rd party libraries at run time (i.e. on Windows, the other libs will be available as DLLs whereas on Linux / OS-X etc they'd be shared objects, which are similar). For the sake of argument, one of those libraries is called "jack".
Obviously, our app can't guarantee which version of the other libs will be on the user's system (or even that they'll be installed at all). So our code is littered with statements like this:-
Code:
if (!jack_port_type_get_buffer_size) {
warning << _("This version of JACK is old - you should upgrade to a newer version") << endmsg;
} else {
some_var = jack_port_type_get_buffer_size();
}
We link to the latest version of jack, where that symbol is declared like so:-
Code:
size_t jack_port_type_get_buffer_size();
One problem is that it doesn't even seem to be an exported symbol (although that wouldn't affect the other platform builds). But apart from that, our customer might have an old copy of jack installed or even no copy! We seem to be making the assumption that if our customer has an up-to-date version, jack_port_type_get_buffer_size will be set to a valid address - but in all other case it'll be magically set to zero (there's nothing in our code that sets it to zero).
View 5 Replies
View Related
Apr 24, 2013
I wonder how can I improve my validity check for user input? I only want them to key in certain range of digit.
Also for my validity check, when I key in character such as ABC, it lead to infinity loop
Here is my code : Code: /*Write a program that can calculate user's age by getting user input their birth date.*/
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int yyyy, mm, dd; //year, month, day
int i = 0; //for the sake of validity check
[Code]...
View 3 Replies
View Related
Dec 22, 2012
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) {
char * tmp = new char[1000];
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
View 9 Replies
View Related
May 23, 2013
Is there a maximum capacity for stdin ?
View 2 Replies
View Related
Mar 26, 2014
I am aiming to read an integer from stdin(pointed to the uart) and echo it with the uart interrupt service routine. there is a simple retarget file along with the main code shown below. So far i can read chars (char x[32] but i am struggling with int.
I have gathered that i need to use the scanf function to read an int from the pointer defined in fgets.
My output is giving me weird values, i enter 8 and ill get a random 3 digits back. I have a feeling its a problem with the input buffer.
Code:
//------------------------------------------------------------------------------// Cortex-M0//------------------------------------------------------------------------------
#include <stdio.h>#include <time.h>#include <rt_misc.h>
#include <stdlib.h>#define AHB_LED_BASE
0x50000000#define AHB_UART_BASE
0x51000000void UART_ISR(){
[Code] .....
View 9 Replies
View Related
Feb 19, 2015
I'm learning internet sockets right now and to that end I've made a simple client/server chat program centered around select(). I've got it to where multiple messages can be sent and received on either side and the "prompt" will move down 1 line each time accordingly.
My only sticking point is when someone is in the middle of typing a message and a new message is received. The message they are currently typing is going to be deleted, so they'll have to start over again. What I want to do is grab the current contents of the stdin buffer (meaning, there's no ), save it, print the received message and move the prompt downward as usual, and then put that saved message back into the buffer, meaning not only is it back on the screen now, it's erasable too as if nothing ever happened.
I know that this will definitely be some very very non-standard C, and that's fine. To that end, I've read that curses, GNU readline, and termios are possibilities for this. I've tried those, but am having trouble making it work.
This will be a moot point when I put a GUI on it soon (probably wx, but maybe Qt) since it won't even be an issue, but I'm determined to make this work. Both systems (the "client" and the "server") are Linux, one being Ubuntu and one being Debian.
View 2 Replies
View Related
Sep 12, 2014
I thought maybe there was something in C that could read full sentences from stdin
Code:
scanf("%100[^
]s", string);
But that's not working for me. so i came up with my own function and its not giving me the results i want. here is the function including the call from main:
Code:
/* * * * * * * * * * * * * *
* FROM MAIN *
* * * * * * * * * * * * * */
printf("
Adding a new part...
");
printf("Enter part name: ");
get_string(new_part.pname);
[Code] .....
/* * * * * * * * * * * * * * * * * * * * * * *
* UNDESIRED OUTPUT *
* * * * * * * * * * * * * * * * * * * * * * */
8newpart
Where is the eight coming from? i thought fpurge clear the buffer. Also, I'm trying to add spaces in between words... i thought maybe putting within the while loop but outside of the if statement string[length +1] = '' would work, but it doesn't. so i put it outside of the loop but that i knew that wouldnt work either.
Problem #2 is reading from a file.. so far i have the following code which reads everything perfectly except the .txt file has a new line character at the end and i think its reading it:
Code:
/* * * * * * * * * * * * * * * * *
* READS FROM FILE *
* * * * * * * * * * * * * * * * */
if(read_in != NULL)
{
while ((fgets(read_string, MAX_PARTS ,read_in) != NULL) && (array_position < MAX_PARTS))
[Code] ....
0 in stock i want it to stop after reading the ball bearings line. a lot of issues for one post, but all related to reading inputs so i put it all on one.
View 2 Replies
View Related
Mar 8, 2013
I'm writing a program that reads from stdin and prints it back out. I'm getting a bunch of garbage then a seg fault.
Code:
double numbers[256];
memset (numbers, 0, sizeof (numbers));
for (;;) {
double num = scanf ("%255lg", numbers);
if (num== EOF)
break;
}
for (int i = 0; i < (int)sizeof (numbers); ++i) {
printf ("%g
", numbers[i]);
}
View 3 Replies
View Related
Sep 14, 2014
Is it possible to create a dynamic char array on the fly directly from stdin? I do not want to create a fixed length array before hand and then copy contents of it into a malloc created array.
Code: //[1]
char line[MAX1];
gets(line);
[Code]....
I could do either [1](buffer overflow problem) or [2] and then goto [3]. But both will have a problem if the input is more than the size MAX1(use defined).
Is it possible to do something of the effect of readLine() method of BufferedReader class in Java or the Console.readLine in .NET? Is it possible to peek into stdin and see the size of the input and then creat an array of the same exact size?
View 8 Replies
View Related
Oct 10, 2014
I want to read a string of unknown length from stdin. I tried to follow the approach from this link. URL....My code is like this:
#include <iostream>
#include <string>
using namespace std;
int n;
cin >> n;
cout << "The value of n is " << n << endl;
}
[code]......
What I have noticed is that if I take integer input from cin (cin >> n;) in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.What is the best way to read from console multiple strings of unknown length in combination with the integers?
View 5 Replies
View Related
Apr 25, 2014
I use the following code segment to read and output the text piped to a program. If there is no piped text, the code segment is skipped and then the program continues. What I need to do is restore stdin to the keyboard after reading from the pipe. I use the PeekNamedPipe() and ReadFile() because reading from stdin blocks until it has something, and I don't want that to happen. After reading from the pipe, the program begins to execute, and the main loop can be paused and it prompts for a command. I just can't figure out how to restore input to the keyboard. Obviously the platform is Windows.
char char_buffer[1024];
DWORD bytes_read;
DWORD bytes_avail;
DWORD dw;
HANDLE stdin_handle;
bool is_pipe = false;
[code]....
View 4 Replies
View Related
Mar 20, 2014
I have a pre-declared array which sorts strings to it's alphabetic order and want to change it so it reads from stdin.
char *array[] = {"aaa", "ccc", "bbb", "ddd"}
I tried doing something like this:
for (i = 0; i < length; i++)
scanf("%s", &array[i]);
I just can't bring it to work. Another thing is, the input is a a bunch of strings separated by commas and ends with a period. Since I have to make a working C model which gets translated to assembly language later on I can't use functions like strtok.
View 6 Replies
View Related
Nov 10, 2013
I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below
rhhrkkj$hghjhdf$ddfkrjt
While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.
Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.
View 6 Replies
View Related
Sep 19, 2013
I usually check if a bit is set using:
Code: bit = (number >> n) & 1; where `n` is the bit I want to check...
But I came across a stackoverflow answer saying:
bit = number & (1 << x); will not put the value of bit x into bit unless bit has type _Bool (<stdbool.h>).
Otherwise, bit = !!(number & (1 << x)); will..
So why is this? why the double !?
View 5 Replies
View Related
Nov 4, 2013
Everything seems to be correct from my perspective. heres the program: Code: /*c program to check whether a string is palindrome or not*/
#include<stdio.h>
#include<string.h>
int main(void) {
char str[30];
int i,j,flag=0;
[Code] .....
View 1 Replies
View Related
Dec 8, 2013
The reason being is that it says that my program is right
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define NFlights 10
struct date {
int month;
int day;
int year;
int hour;
int minute;
[Code] ....
View 6 Replies
View Related
Jun 15, 2013
Q. In context of C language syntax checking, which of the following can be modeled using Finite Automata?
(A) Detecting proper termination of an instruction.
(B) Detecting balance of parentheses.
(C) Detecting initialization of a variable.
(D) None of the above.
View 4 Replies
View Related
Jan 31, 2015
I am new to C++ , i want to know how to check if a key with modifier is pressed.
When I use GetAsyncKeyState() it gives me error identifier not found.
View 1 Replies
View Related
Aug 4, 2013
I think I may have found a new way of checking for 3d polygon collisions, but I'm not sure. The method involves...
1. finding the planes that the primitives lie on
2. finding the line where the planes intersect
3. if both polys have points on both sides of the line AND have points that overlap on the 1d space of the line, then they intersect.
I have some half done code testing this, and so far it seems to be sound and fairly fast. These are some average time-tests done on my machine for each part:
1. 30 microseconds (both)
2. 7 microseconds
3. TBD
View 5 Replies
View Related
Jun 17, 2014
I have a question, how can I check if a program is running using c++? For example
if (notepad.exe is running) {
.... ..... ....
View 3 Replies
View Related
Jul 20, 2014
How would I go about checking for deallocated memory?
For example, let's take this into consideration:
// Unsigned 32-bit / 64-bit integer: uint32, uint64
uint32* Pointer = new uint32[ Size ];
uint64 MemAddr = ( uint64 ) Pointer;
delete[] Pointer;
The above code would proceed to create a new array, store it in a pointer and retrieve the memory address of the array before finally deleting the array.
So let's assume we re-build the pointer and try to access the now deallocated array:
Pointer = ( uint32* ) MemAddr;
Pointer[ 0 ] = 0;
Based on the above snippets of code, how would I check "Pointer" after rebuilding the memory to check if the rebuilt memory has actually been deallocated. Without a check we'd get an exception error.
A bit of detail on why I am trying this:
Before thinking up how to do this, I was storing the addresses in a list and check the list for the addresses to see if they existed or not. However this requires an O(n) search, which isn't exactly what I am wanting. So instead if I used a check for deallocation method, I can go for an O(1) time check and reduce the total time it would take to check for memory allocation/deallocation.
View 11 Replies
View Related
Jan 23, 2014
I have created a prompt which prompts the user for an integer and I have set up a loop to check for if it is an integer or not. My "bug" is that a user can enter an "integer" and "space" and "enter" and it does not give any error and assumes that "All is FINE!". I have gotten the value from the ascii table of 'SPACE' and put it as a check in my parameter of while, but it does not work.
Here is my code:
int x, y, boolean, i;
char buff[256];
printf("Enter the first integer value: ");
scanf("%s", buff);
i = 0;
boolean = 0; //initializing our boolean var that will eventually decide if we have an error or not
[code]....
View 4 Replies
View Related
Mar 6, 2014
Suppose you have a templated class, such as
template <typename T>
class Matrix {
// some stuff and some methods
};
and let's say that you have some methods that need to do some type-dependent stuff, like, for example,
template <typename T>
Matrix<T> Matrix<T>::transpose() const {
// get this->rowCount, this->columnCount
// create a Matrix that has rowCount amount of columns and columnCount amount of rows
// copy (*this)[j][k] to theMatrix[k][j] (for all of the entries in *this)
// if the entries are complex, take the complex conjugate of them all
}
Would it be good practice to check explicitly for the typename parameter (or is this, somehow, defeating the purpose of templates)? std::cout << "I know that this is a design question, but it needs to be asked... ";
View 1 Replies
View Related