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
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
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
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
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
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
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 '