C/C++ :: Determine Input Type Without Converting

Sep 19, 2014

Originally I had to create a simple integer palindrome program that looped while the user entered 5 digit inputs (entering -1 stopped the loop). I did this using a conversion to string, reading the length to determine if the length was valid, and then reading the string forward and backwards inside of a while loop. (snippet below)

while( digitsEntered != -1)//Allow user to quit by entering -1 to end the loop
{
ostringstream convert;//conversion stream
convert << digitsEntered;//converted text from number goes in the stream
convertedString = convert.str();//store the resulting conversion to convertedString

[Code] ....

The next stage of this program was to do the same thing with strings instead of integers. However, the option to end the loop by entering -1 is still a requirement.

I think the way to do this is to first determining whether the input is a string or an integer, and if it is a string then read it and if it's an integer determine if it's -1. However, whenever I write code to do this, it converts strings to 0 so the string is not stored and cannot be read to determine if it is a palindrome. Is there a way to determine the type of input without converting it into a different type i.e. read string and then keep string or read number and keep number?

View 3 Replies


ADVERTISEMENT

C++ :: Determine If Type Is Of More Derived Type Than Another At Runtime

Aug 31, 2014

I have a function like this:

template<typename T>
void f() {
//...

[Code]....

list contains, in order: A, B and C in any order, D, E

I am thinking it is possible with some clever template and polymorphism combos, but maybe not. As a last resort I know how to make it work by storing static type information in each class, but I'd like to avoid that if possible.

View 6 Replies View Related

C++ :: Sentinel Value To Determine Type Of Character

Apr 24, 2014

Write a program that will prompt a user to enter a single character, the prompting will continue till a sentinel value is entered. For each character entered perform the following tests and print out a relevant message if the character passes the test. Print out a default message if the character does not pass any of the tests.

Tests that should be in program: Punctuation, Upper Case, Digit, White Space.

Sample Run: “A”, “a”, “7”, <tab>, “?”, “$”

So far I have the following code done. The problem is that when I run the program, the first character is correctly identified. However, every character afterwards is defined as a whitespace character.

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main() {
char input;
char response;

[Code] .....

View 2 Replies View Related

Visual C++ :: Determine Type In Function Template

Jul 28, 2013

I want to detect the type in a function template, like this:

template <class myType> myType Function (myType a, myType b) {
//Detect the myType
If (myType is int)

[Code] ......

Is that possible?

View 6 Replies View Related

C/C++ :: Error - Insufficient Contextual Information To Determine Type

Aug 16, 2013

I have part of a class that checks to make sure there is a fault that lasts for 60 seconds. The code below is written several times throughout the class for different subsystems dealing with overcurrent.

// Over Current
if (UUV->getCTaps(MOTORCURRPOS_1) > 1.4*UUV->getcurrMode.getMotor().Peak) // The fault { 
if (motor1Timer == NULL)
motor1Timer = time(&timer);
else if ( time(&timer) - motor1Timer >= 60)
motor1Over = true;
} else
motor1Timer = NULL;

The timing statement is okay because it works on all of the other fault checkers. It is the if statement that is causing the error I just do not know why.

View 1 Replies View Related

Visual C++ :: How To Determine Cross Platform Type During File Save

Nov 10, 2014

I am overriding OnSaveDocument in my MFC document class to strip out the carriage returns when saving my app's document to a UNIX file system but not when the user is saving a file to a Windows file system.

Is there a way to determine if the lpszPathName in OnSaveDocument(LPCTSTR lpszPathName) is a UNIX or Windows file system?

Note, I want to avoid hard coding server names and I want to avoid overriding the FileSave dialog and forcing the user to select Windows or UNIX.

View 6 Replies View Related

C :: Read String From Input Then Determine Which Character Is The Largest

Jan 12, 2015

I'm very new to c programming and I have some background in C# and java. I am supposed to read a string from input then determine in that string, which character is the largest, i.e. I think b>e and e>z, e.t.c. If the string is empty I should return ''.

I haven't done any programming in C before and I don't know how to handle strings and characters in C.

View 2 Replies View Related

C++ :: Take User Input Selection Of Columns And Determine Array

Aug 29, 2013

I am writing code for a program that will take user input selection of columns and determine an array based on that.The number of columns will be user selected.The number of rows equals 3^(columns) <--exponent not XOR

- This is because each column has the possibility of having the numbers 0,1,or 2

For example, if the user were to select "4" columns, then the array would have 4 columns and 3^4=81 rows. Then I would like to populate this with all of the possible combinations of 0,1,2

i.e.
0000
0001
0002
0010
0011
0012
0020
0021
0022
0100
....
2220
2221
2222

how I would create the "For" Loop for this?

View 19 Replies View Related

C# :: Converting Typeof To Instance Type?

Sep 29, 2014

I'm trying to send out a message to all classes that are a Director. The only issue I'm running into is converting the typeof the object so the receiever parameter can hold it.

Here's my code for sending the message:

messageDispatcher.DispatchMessage(new Telegram(typeof(Director), this));

Here's where the receiver of the message gets assigned:

private IOwner receiver;
private object message;
public Telegram(object receiver, object message) {
this.receiver = receiver as IOwner;
this.message = message;
}

I tried using the Activator class to instantiate the object but was running into issues with getting it to accept the object passed. Here's what I had had:

this.receiver = (IOwner)Activator.CreateInstance((Type)receiver);

No matter what I've tried the receiver ends up being null. How could I go about assigning the type correctly to it?

View 9 Replies View Related

C++ :: Program To Determine Number Of Inputted Riders And Closes When Input Is -1?

Jan 16, 2015

I seem to be having a logical error but can not find the sources.

#include <iostream>
using namespace std;
int main() {
int student = 0;
int adult = 0;

[Code] ....

View 1 Replies View Related

C :: Converting Char Array To Int16 Type

Apr 18, 2013

I am trying to convert a char buffer[1024] with raw data to int 16 buffer2[1024] which is supposed to have hex data (0x01, 0x02 kind of format).

Code:
char temp_buffer[1024];
block_size = sizeof(temp_buffer);
num_blocks = sizeof(char);
Status = mount(0, &myfsObject);
if(Status != DAVEApp_SUCCESS)

[Code] .....

I am verifying the data of resultant buffer by creating a new file and writing data to it. The result which i got is:

Code: H uy H uy H uy H uy H uy H uy H uy H uy

View 2 Replies View Related

C# :: SQLException - Error Converting Data Type Varchar To Numeric

Oct 22, 2014

I am writing a simple program to suck in a txt file then pump it into sql.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;

[Code] ......

How I can get past this error and get the data into sql? I read a couple articles on .tag but not sure I understand what to do.

View 11 Replies View Related

C++ :: Read User Input To Determine Different Discounts By Number Of Units Sold - Errors In Program

Jun 23, 2014

it will not run and im not sure why. I have a couple of errors, but I'm not sure why.

Here is my code.

//Reads input from user to determine different discounts by number of units sold

#include <iostream>
#include <string>
using namespace std;

int main() {
//Declaration and Initialization of variables
int quantity;
double discount,price = 99.00,totalCost;

[Code] ....

View 1 Replies View Related

C++ :: Deal With A Failed Input Stream And Type Checking Input?

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

C++ :: Converting A String Input To Char?

Feb 8, 2015

I am writing a program that asks the user for their gender. I know that atoi converts arrays into integers, but I need the input from the user in the form of F or M into a char, and return it to the main function to be displayed at the end of the program.

string Employee::getgender(char gen)
{
cout << "Please enter your Gender: " << endl;
//atoi function would go here, what for char?
getline(cin,gen)
return gen;
}

View 1 Replies View Related

C++ :: Converting User Input Int To String Output?

Apr 22, 2014

I am writing a code in a class that will change the user input which is integer to a string. For example if the user enter 13347..the output should change to "one three three four seven" on the user screen.I'm not getting the right output.

Code below:

#include <iostream>
#include <string>
using namespace std;
string arr[]={"one","two","three","four","five","six","seven","eight","nine"};
class convertTo{
public:
int signed num;
int convet(){
cout<<"Enter a number to convert to string "; cin>>num;

[code]....

View 2 Replies View Related

C/C++ :: Converting Uesr Input Int To String Output

Apr 22, 2014

I am writing a code in a class that will change the user input which is integer to a string. For example if the user enter 13347..the output should change to "one three three four seven" on the user screen.I'm not getting the right output.

Code below.

#include <iostream>
#include <string>
using namespace std;
string arr[]={"one","two","three","four","five","six","seven","eight","nine"};
class convertTo{
public:
int signed num;

[Code] ....

View 5 Replies View Related

C++ :: Text Adventure Game - Converting Input To Uppercase?

Jul 16, 2014

I'm trying to create a sort of text-adventure game, but rather than having to check for the input being uppercase or lowercase, I'd rather convert it before checking it in an if statement.

I've created a function, and I know it works because when I print the output, it shows up capitalized, however when I check it with an if statement, it doesn't pass.

Here is my code:

#include <iostream>
using namespace std;
void stringUppercase(string x) {
int i;
for (i = 0; i < x.length(); i++) {

[Code] ....

View 4 Replies View Related

C++ :: What Data Type To Treat Input As

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

C++ :: Finding Out Input Data Type?

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

C++ :: How To Check For Data Type When Input Value Into Program

Apr 11, 2013

How would I check for proper data type when someone is to input a value into the program? Ex:

int i;
string a;
cout << "Enter a number: ";
cin >> i;
cout >> "Enter a string: ";
cin >> a;

How would you check to make sure that int i would be an actual number and not a letter like "a"?

View 4 Replies View Related

C++ :: Looping User Input For Specific Data Type

Apr 17, 2015

Here's my code so far:

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int dayNumber = 1;

[Code] ....

The code, when run, prompts the user to input an integer like this:

Code:

Day 1
Andy :>12
Bill :>7
Charlie :>15

Day 2
Andy :>5
Bill :>25
Charlie :>14
.
.
.
etc.

Ok, so the code prompts the user to enter an integer for each of the 3 persons and then increments the "Day" and so on... When the user enters a negative value (-1, -2, etc.), the loop is broken and the program ends. Alright, everything is hunky-dory so far. 2 things which I can't figure out.

1. when the user doesn't enter anything and just hits return, the cursor drops a line and is still awaiting an input but doesn't show the prompt "Andy :>", for example. How can I get it to repeat the last prompt when the user doesn't enter anything? Like:

Code:

Day 1
Andy :>
Andy :>
Andy :>12
Bill :>25
Charlie :>15
.
.
etc.

2. When the user enters a letter or a special character, the program blows up. How can I correct this?

I've tried changing the data type for the variable used for the input, tried using getline, get, etc. With my current beginner knowledge, I'm missing something. So how can I get this to work?

The program should allow only integers to be entered, while allowing a negative number to trigger the loop to break or the program to end, and while re-prompting the last person if the user entered an invalid input.

View 13 Replies View Related

C++ :: Overloading Input Operator For Rational Type Objects To Work

Nov 13, 2014

I cannot get my function overloading the input operator for rational type objects to work.

lab9.cpp: In function ‘std::istream& operator>>(std::istream&, const rational&)’:
lab9.cpp:186:20: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from expression of type ‘rational’
return (inputFrac);

lab9.h

#include <iostream>
using namespace std;
class rational {
public:
rational();
rational(int a, int b);

[Code] ......

View 4 Replies View Related

C++ :: Effect On Output Of Program Of Different Numbers Input To Int Data Type Named

Mar 2, 2014

// this program gives random number output
#include <iostream>
#include <cstdlib>// contains function protype for rand
#include <iomanip>// for setw
using namespace std;

[code]....

what is the effect on output of program of different numbers input to the int data type named seed*/

View 1 Replies View Related

C/C++ :: Comparing Input String To Type String Vector

May 29, 2014

I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.

vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}

View 3 Replies View Related

C++ :: Changing Array Of Numbers Of Type Char To Type Int?

Apr 27, 2013

I'm having some problems with changing an array of numbers of type char to type int. Every time i try to sum 2 array indexed values it returns some letter or symbol. Also, if i change the type of the array in the functions the compiler gives me an error message. I would also like to add that the problem requires that the first two arrays be char so each individual number gets assigned to a different value.

My current code is:

Code:
#include <iostream>
void input(char a[], char b[], int& size_a, int& size_b);
void convert(char a[], int size);
void reverse(char a[], int size);
void add(char a[], char b[], int c[], int size);
int main()

[Code]....

View 4 Replies View Related







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