C++ :: Preventing The User From Feeding (Cin) Unexpected Values

May 2, 2013

I'm just starting c++, doing tutorials. So many tutorials. I've noticed that the tutorials all assume the same thing: That the user will always do exactly as he/she is told when asked for input.

Example: "Please enter your age:"

Now, the example code might be expecting the user to type some numbers, but what if the user feels like typing out the letters of their age?

"I am ninety five thank you very much, sonny"

I could specify to the user that I only want the information in number form, but what if the user is just being a dick?

What if the user types, "none o' yer business."?

So... how to approach "fool-proofing" player input?

View 2 Replies


ADVERTISEMENT

C++ :: Preventing Execution Of A Function

Jun 26, 2013

PHP Code:

class MyClass{};namespace BI{    class BusinessInterop {    
public:        static MyClass* func( {
printf("BusinessInterop");return new MyClass();}    

[Code] .....

I would like to know how to prevent the program print out "Business", I'd like it to call the base class's static function. I have tried to changed MyClass* into void* but it still prints Business :grumpy:.

My challenge is to output "BusinessInterop" without any changes made to the polymorphic structure as designed.

View 7 Replies View Related

C :: Preventing A Buffer Overflow When Strings Being Passed As Arguments

Sep 21, 2013

preventing a buffer overflow when dealing with strings being passed as arguments.

If I have a function prototype such as:

Code:

void foobar(char *bar);

That argument bar - is intended to take a pointer to a buffer of x characters in length. Inside that function, I can't get the size of that buffer, as bar is now just a pointer to a char. I COULD just make the user of this function pass a length parameter, but there is no guarantee that would be correct. Is there a bullet proof way of detecting that the user has provided a buffer that is too small?

View 11 Replies View Related

C/C++ :: Compare Values Stored In First Array To User Inputted Values In Second Array

Oct 19, 2014

Goal: Write a program that compares the values stored in the first array to the user inputted values in the second array.

In order to fix this error: [URL]...

I had to change my array initialization to one with a star in front of it:

char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
to:
char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

I also changed my 2nd array to one with a star in front of it: char *a2[20];

What does this mean exactly? Putting a star in front of an array?

Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:

cin>>a2[i];

View 3 Replies View Related

C :: How To Get Values From The User And Add Them To Equation

Oct 30, 2014

I have a small project for school due today but i cant get my head around programming

Ive to create a program where ive to get the values from the user and add them to an equation. I have also to add a selection statement where it will ask for the material being tested which will have its value set in the program.

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

C++ :: Insert User Values Into Database

Jun 25, 2014

I'm trying to insert into the database I created, values entered by the user. But I can't find how to insert them it looks like this:

//Go for the user infos
cout << "What is your ID ? : ";
cin >> id;
cout << "What is you letter ? : ";
cin >> letter;
cout << "Your letter is "<<letter<< " and your ID is "<<id<<"

[Code] ....

I don't know how to proceed, I receive a error here for the sql2 statement

error C2296: '<<' : illegal, left operand has type 'const char [36]'

View 12 Replies View Related

C# :: Compare Values From User Input

Nov 3, 2014

What do I put to have UserNumber == 1-10? In other words I want it to say...

if UserNumber equals 1 through 10 <<<<This is where I'm having the issue.

Do this
Else
Do this

namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Start:
Console.WriteLine("Please enter a number between 1 and 10");
int UserNumber = int.Parse(Console.ReadLine());

[Code]...

I got it.

View 8 Replies View Related

C/C++ :: Beginning Structures Take Values By User?

Dec 14, 2014

I'm trying to learn structure type in C. In this trying, the code must take 9 value from user but it takes 6 values then returned. I scrutinized but I can't find my error.

#include <stdio.h>
typedef struct books {
char name;
float price;
int pages;
} book;

[code].....

View 2 Replies View Related

C++ :: How To Make Array Which Stores Values From User

Sep 22, 2013

How to make an array which stores the values i'm getting from the users?

For example if i have a function which allows the user to give me values, how can i store them in an array.

View 4 Replies View Related

C++ :: Can't Store Different Values Into Vector By User Input

May 4, 2013

I can`t seem to store multiple values into my vector when a user keys in an input. Lets say a user inputs 1,2 as the first set and later keys in 3,4.

My vector gets overridden by 3 and 4. I'm trying to pass these values into a function to store them into a vector and use them in the function.

Below is my code snippet

int reconstructSecret(int x, int y) {
int getX,getY;
getX=x;
getY=y;
vector<int> myXCordVec;
vector<int> myYCordVec;

[Code] .....

View 6 Replies View Related

C/C++ :: Finding Sum Of Values Entered By User In While-loop?

Aug 26, 2014

Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered. When the loop ends, print the smallest, the largest, the number of values,and the sum of values. Note that to keep the sum, you have to decide on a unit to use for that sum; use cm.

View 9 Replies View Related

C++ :: Set Width Of Data Values Which User Will Input

Sep 24, 2014

I am trying to set the width of the data values which the user will input when using the program but I don't know how to get it to show the values when I tryto set the width of the variables in a nice column .

//Programming Assignment 4

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main() {
double q1r1, q2r1, q3r1, q4r1;
double q1r2, q2r2, q3r2, q4r2;
double q1r3, q2r3, q3r3, q4r3;

[Code] ....

View 2 Replies View Related

C++ :: Unexpected Output In Example Function

Jan 21, 2015

I am currently trying to understand why this example for using an array as an argument in a function has a different result than what the lecture notes say it should be.

So supposedly sum should return with the value 28, but I get 27 no matter what. I also am not very good at reading and understanding what exactly the order of operations for this function are.

Code:
#include <iostream>
using namespace std;

int sum(const int array[], const int length) {
long sum = 0;
for (int i = 0; i < length; sum += array[++i]);
return sum;

[Code] ....

View 3 Replies View Related

C :: Unexpected Output With Char

Sep 14, 2013

I tried running the code below and i got an unexpected output

Code:

#include<stdio.h>
void main() {
char a='A';
while(a)

[Code] ....

The code is supossed to give an infinite loop but instead it terminates with a=0...I tried running it with some casting like this

Code:

#include<stdio.h>
void main(){
char a='A';
while((int)a)

[Code] ....

But the output was the same as before.why the code has this unexpected behaviour???

View 7 Replies View Related

C++ :: Unexpected SIGABRT At Return 0

Jun 24, 2013

I am working through Binary IO with objects and am running into a curious error.

The program executes perfectly until it hits the very end. At "return 0", the program fails. The debugger picks up SIGABRT when executing "return 0". This seems to me to indicate a deconstructor problem of some kind. However, I can't seem to find any deconstructor problems (I am fairly new at programming though).

If I comment out the following two lines:

"binaryio.read(reinterpret_cast<char *>(&studentNew1), sizeof(Student));"
"binaryio.read(reinterpret_cast<char *>(&studentNew2), sizeof(Student));"

then the program finishes exiting without error . . . . but the whole point is to be able to read from the binary file. With those two lines in the code, the program successfully reads from the file and outputs the objects to the console ,but fails at "return 0";

Here is the code:

#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;

void displayStudent(Student student) {
cout << student.getFirstName() << " ";

[Code] .....

View 5 Replies View Related

C++ :: Unexpected Output From Fprintf?

Feb 11, 2014

I have the following piece of code which should write the contents of my vetor to y file but I am getting a very weird output in my file.

for (int i=0;i<amount;i++) {
fprintf(pFileO,"Case #%d: ",i+1);
for (int j=0;j<words[i].size();j++) {

[Code].....

As you can see this doesn't make sene because the file should also contain the exact same things as the cmd outputs. What's going on here ?

View 1 Replies View Related

C# :: Update Initial Values Once User Has Added To Inventory

Apr 10, 2015

I am having some trouble creating a listbox in visual studio. I declared an array, set initial values, and now I would like to be able to update those values. It is a pizza inventory app, so I need to update the initial values once the user has added inventory. This is the code I have for the "Update Inventory" button:

// read in value and convert to double
double dblInput = Convert.ToDouble(txtInput.Text);
// loop through ingredients and add inventory to selected ingredient
for (int i = 0; i < dblInventory.Length; i++) {
// is item at i checked for update?
if(lstInventory.GetItemCheckState(i) == CheckState.Checked)

[Code]....

View 11 Replies View Related

C++ :: Getting Unexpected Outputs For Bitfields And Floats

Aug 7, 2013

I'm getting unexpected output in 2 different cases. The 1st deals with bitfields. The C++ standard has this line about integral promotions:

An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the
bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.

If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

This sounds like the value of a bitfield will always be treated as a signed int if the signed representation of the value will fit in the bits. This seems to hold true for my C compiler, but not my C++ compiler.

I tried storing a small negative value in a bitfield that has enough bits to store the sign bit and the value. But when I print out the bitfield, I always get a large number

In the example code below, I expect the output:

Code:
foo.x = -1
foo.y = -2
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = 4294967295 But I get: Code: foo.x = 31
foo.y = 6
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = -1 -------------------

The other issue I'm having is sort of similar. I'm trying to store 4294967295 into a float, but when I print it out, I get 4294967296. i've tried storing a few other large values like this and what's printed out is rarely the value I stored. I thought it might be because of some int to float conversion, so I tried 4294967295.0. Still no luck. Then I remember that defaults to a double so maybe that's the issue so I tried 4294967295.0f. Still no luck. Why can't I store the correct value here? I don't think it's an IEE format thing since I can use these values as floats on a calculator program.

The example code showing both issues is below.

Code:
#include <stdio.h>
typedef struct {
signed char x : 5;
signed char y : 3;
}my_struct_t;

[Code] .....

View 11 Replies View Related

C++ :: Unexpected Expression Compiler Error

Jul 30, 2013

My compiler (GCC) keeps expecting an expression where it shouldn't in 1 specific piece of my code:

int zxcNewWindow( HWND parent, TCHAR *text, zxWINDOW *kid,
UINT style, int x, int y, int w, int h, int type )
// right here
{
*kid = zxDefWINDOW;

The project contains only 2 files right now and the settings are just the default for an empty Code::Blocks 12.11 project. Both files are in UTF-8 format (tried in ASCII too), I just cannot see why this is not compiling correctly. I'll post the files in the next two posts.

Edit: For those of you who didn't get what the error was from the above here's the full log:

mingw32-gcc.exe -Wall -g -DzxDEBUG -c C:MePrjscppzxGUImain.c -o objmain.o
C:MePrjscppzxGUImain.c: In function 'zxcNewWindow':
C:MePrjscppzxGUImain.c:39:10: error: expected expression before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)

View 8 Replies View Related

C++ :: Reverse Array - Unexpected Result

Jun 20, 2013

i use dev c++...i write this code to reverse an array and save the result in the same one

if n=3 i expect
a[0]=0 a[1]=1 a[2]=2 (before rev is OK but after calling rev)
a[0]=2 a[1]=1 a[2]=0 (expected result )
but i get
n=3

[code].....

View 4 Replies View Related

C :: Program To Get Series Of Integers From A User And Storing Those Values Into Array

Feb 10, 2013

The problem deals with writing a program to geta series of integers from a user and storing those values into an array. Then use a function called selection_sort, when given an array with n elements, the function must sort the array from smallest to largest values.

I have code, and im trying to get it to compile but im getting these implicit declaration errors and conflicting types. Here is my code and the compiler errors.

Code:
Compilation started at Sun Feb 10 20:14:48

gcc -Wall -o ex9-1 ex9-1.c
ex9-1.c: In function 'main':
ex9-1.c:16:5: warning: implicit declaration of function 'selection_sort' [-Wimplicit-function-declaration]
ex9-1.c:20:2: warning: implicit declaration of function 'prinf' [-Wimplicit-function-declaration]
ex9-1.c: At top level:

[Code] ...

Compilation exited abnormally with code 1 at Sun Feb 10 20:14:49

Code:
#include <stdio.h>
int main(void) {
int a[100], i, j, N;
printf("How many numbers will you be entering: ");
scanf("%d", &N);

[Code] .....

View 4 Replies View Related

C++ :: Regex Unexpected Matching (Floating Point)

Oct 28, 2014

I have written this regex to match a floating point literal:

(^[[:space:]]*)(([0-9]+.?[0-9]*([eE][+-]?[0-9]+)?)|"
"(.[0-9]+([eE][+-]?[0-9]+)?))([fFdD]?[[:space:]]*)$

and when I match it with string like "123e" or "e2" it works while it shouldn't and I can't find the reason why.

View 2 Replies View Related

C++ :: Unexpected SIGSEG Abort When Working With Vector

Oct 9, 2013

I keep getting a segmentation error when ever I have the following code...

int main(void) {
//Section 1
unsigned long val = 12;
std::vector<unsigned long> vval;
for(unsigned long i = 0; i < 100; ++i) {
vval.push_back((unsigned long)0);

[Code] ....

Error: *** Error in `/home/alex/projects/bignum/build/bignum': free(): invalid pointer: 0x00007ffff75b5b88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7274a46]

compiler is clang++ 3.2

It doesn't happen if I restructure it so that bignum::num is not a pointer to an std::vector<unsigned long>

View 10 Replies View Related

C/C++ :: Unexpected Behavior For Classes That Contain Member Object

Apr 1, 2014

I have a WordRecord that contains a LinkedList (both my doing). I have rigorously tested my LinkedList class, and know that it works (heck, I used it in the last project I had!). The problem is that undefined behaviour seems to happen when using the WordRecord, which has a std::string and a LinkedList<unsigned>. (The problem happens with the LinkedList.)

Here is the code:

main.cpp
#include <iostream>
#include "BinaryTreeNode.h" // here for test purposes
#include "LexicographicTree.h"
#include "LinkedList.h"//also for test purposes
#include "OutputStream.h"
#include "WordRecord.h"
using namespace std;
int main()
{
// setup the OutputStream to print to "test.txt"
OutputStream stream("test.txt");
// create a sampleWordRecord (make it have the word "I" on line 1)

[code]....

One of the requirements for the project is that it must compile on Unix server (I am using Windows, and have tested it in both environments.) I get a core-dump in the Unix environment. On the other hand, the output on-screen in the Windows environment looks right. However, when I open up the text file, I get the following

Sample word record:

WordLines
I{14}
/* I have no what is happening to sampleWordRecord's LinkedList; I am not trying to modify it, except for where I created the sampleWordRecord! */

View 14 Replies View Related

C++ :: Upon Checking Emptiness Of Array - Getting Unexpected Result

May 9, 2015

node = pool.at(0) is executed even though pool is empty,

I even tried pool.size() == 0, that line is still executed. What is the reason, I wonder?

Code:
NodePoolNode* NodePool::acquireNode(int x, int y, long t) {
std::stringstream key;
key << x << ":" << y << ":" << t;
NodePoolNode* node = usedNodes[key.str().c_str()];
if (node == NULL) {

[Code] ....

View 3 Replies View Related







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