C :: Comparing Character To A Number
May 5, 2013
I created a function to check whether the first character of a string is a number or not by comparing it to every base 10 digit using a for loop. If it is, the string is valid, if it is not, the string is not valid
Code:
//ensure the first character is not a number
int ValidFirstChar(char FirstChar) {
int IsChar = TRUE, DigitCount = 0; /*boolean value indicating whether the first character is a number or not.*/
//check through all base 10 digits
for (DigitCount = 0; DigitCount <= 9; DigitCount++) {
if ((int)FirstChar == DigitCount)
[Code] ....
I have checked for the case where the first character is a number but it is displaying the error message for it. I have tried typecasting the char variable but that has not worked.
View 2 Replies
ADVERTISEMENT
Sep 26, 2014
The following Program ask a user to input a sentence. Program alphabetically sort individual character for each word.
My problem is, how do I ignore case sensitive if a user enters a uppercase letter. I try using stricmp but ran into some errors.
Below code:
#include <iostream>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
void bubblesort(char a[]);
int main() {
char sent[50];
[Code] ....
Current output:
input: today is a great day
output: adoty is a aegrt ady
Using Uppercase:
input: Today is a Great Day
output: Tadoy is a Gaert Day
What I want:
input: Today is a Great Day
output: adoTy is a aeGrt aDy
View 6 Replies
View Related
Feb 25, 2013
I want to compare the part of the character array with the scanned input. I've initialized the character array (colourCompare).
What I want to do is, if the input colour matches up with one of the elements in the colourCompare array, it will then read the next value(I did not include "read the next value part"). If the input does not match up, then it goes back to the scanning part.
Code:
char colourCompare [12][6] = {"Black","Brown","Red","Orange","Yellow","Green","Blue","Violet","Gray","White","Gold","Silver"};
float resistanceCal() {
[Code]....
View 14 Replies
View Related
Feb 16, 2014
Suppose i have a very large number stored as a string, say
std::string str = "1000000000000000000000000000000000001";
And i use std::stringstream to convert it to int like this
std::stringstream ss(str);
uint64_t i;
ss >> i;
Then I would be maxed out right. so how would one practically handle things like comparison of two such numbers.
I could think of 2 approaches :
1) I can compare the numbers character by character.
2) I can put the results of ss >> i; into an array then compare each element of array
would there be any other methods??
View 4 Replies
View Related
Mar 2, 2013
Any way to do it...its only works with the first letter. So i have a txt file with information such as de3 dn5 dn7 dw9 ds1 and how to get the letters and then the number.
example:
if de print this is de; and print the number that follow de
Code:
#include <stdlib.h>
int main ()
{
FILE * pFile;
int c;
pFile=fopen ("de3.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
[Code]...
View 5 Replies
View Related
Feb 13, 2013
how can i display the number of variables or character that the user input?
View 4 Replies
View Related
Nov 30, 2013
I would like to insert some character after each number of my string .. I found how to insert before each character, but what I want is :
string str = "12 + 5"
insert ^ after each number
Output = "12^ + 5^"
View 7 Replies
View Related
May 28, 2014
Im programming a roguelike game using visual c++ Microsoft express 2010 and i made a multidimensional array for my first map. I have the walls as # and was wondering how i could turn those into ascii symbol 219. Also i need to know how to turn specific text certain colors.
View 5 Replies
View Related
May 17, 2014
Everytime I type a character, the number 1 appears in the next line. And i just keep getting the message "Wrong! I have more than that." even when I type a number bigger than 1023
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));
[Code] ....
View 10 Replies
View Related
Feb 11, 2014
The function uses a "for" loop to print the given character the number of times specified by the integer.
How can I make a for loop to do that?
So.. my code looks like this:
// cpp : Defines the entry point for the console application
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void printMyInteger(int myInteger, char myChar) {
[Code] ....
So.. here is my error:
Error1error C2143: syntax error : missing ';' before ')'d:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp101Week 6
Error2error C2143: syntax error : missing ';' before ')'d:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp101Week 6
3IntelliSense: expected an expressiond:workspaceuniversity ools for games and animationworkshopsweek 6week 6week 6week 6.cpp107Week 6
View 3 Replies
View Related
Oct 31, 2014
How do I output a Unicode number if I extract a character off a console or out of a file.
If I do the below, I use the Unicode number to show a character. The below shows me 25² .
char b = 'u00B2';
mystring = "25";
mystring.append(1,b);
How do you go back the other way? If I extract the 25 and the ² separately, how do I get the unicode number for ² ?
View 3 Replies
View Related
Nov 20, 2014
I have an assigment to make program which deletes from sentence all words with character count which is equal to pair number , for example - [ I like C ] and the result of this program should be [I C] because the word like contains 4 characters which is pair and it should be removed.
So I started writing my program and I am stuck at this block of code -
#include <stdio.h>
#include <stdlib.h>
main () {
char text[100], blank[100];
int c=0,d=0,i,j;
gets(text);
[Code] ....
To explain what is happening - I go through all string and search for first ' ' space symbol and check its value. If it is pair then my program prints that it is not pair[because last character before space had not pair number of characters], but the hardest part comes in when i have two not pair words , because space takes one character and now when i check if i%2 == 1 the answer is false [0] for the second word .
View 2 Replies
View Related
Feb 19, 2015
I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
int main() {
int number;
cin >> number; //expecting a number for first line user input
for (int i = 0; i < number; i++) {
string str;
[Code] ....
View 1 Replies
View Related
Sep 14, 2014
I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?
#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {
[code]....
View 6 Replies
View Related
Jul 25, 2012
Double values are stored in text file. 23.5 36.8 34.2 ... My teacher told me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi(). I have to convert it into double. but i dont know how to do this....
View 5 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
Aug 10, 2012
How do I write an a program that will read an input file character by character?
View 1 Replies
View Related
Oct 6, 2013
In my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is
void typeset (int maxWidth, istream& documentIn)
I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file.
View 10 Replies
View Related
Sep 13, 2014
So I'm trying to create a function that replaces any instance of a character in a string with another. So first I tried the replace() string member function:
In my implementation file
void NewString::ReplaceChar(const char& target,const char& entry)
{
this->replace(this->begin(),this->end(), target, entry);
};
Main program
#include "NewString.h"
using namespace ...;
int main()
[Code].....
Instead of replacing the the l's with y's it outputted a long string of y's. Also, NewString is derived from the string class (it's for the assignment). the header and whole implementation file, already tested.
I've also tried, instead, to use a for loop in ReplaceChar() but I need to overload the == operator and I don't know how I should exactly:
bool NewString::operator ==(const char& target)const {
if(*this == target)
return true;
[Code]....
I want the == operator to test if the value in the char array is equal to target but I'm not sure how to pass in the position. I'm guessing the this pointer in ReplaceChar() is not the same as the one dereferenced in ==() because target is never replaced by entry in the string.
View 5 Replies
View Related
Jul 4, 2014
Can we do this :
Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
temp=strings[1];
View 3 Replies
View Related
Oct 7, 2013
I'm a novice with C programming and i have to solve an error in the following code. The code works like you enter a password called "uoc" and it shows as OK. But surprisely when you entered another password as "Cambridge" it works fine too.
I think that the problem is in the array declaration but i'm checking resources and no success!
Code:
#include <stdio.h>
#include <string.h>
struct {
char str[8];
char ok;
} data;
[Code] ......
View 3 Replies
View Related
Feb 24, 2013
Given this code
Code:
double x=1.00,y=2,z=4;
if (y/z||++x)
x+=y/z;
printf("%f
",x); So (y/z||++x)
is true if at least one expression is true, that is either (y/z)!=0 or (++x)!=0 or both. I wonder how the comparison is done? Is (y/z) be truncated to integer or 0 be promoted to double?
View 2 Replies
View Related
Mar 6, 2015
how to compare each element of array with another,defined ? I have tried this,but didn't work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bill()
[Code].....
View 3 Replies
View Related
Mar 25, 2013
I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 100
#define COLS 4
}
[code]....
View 4 Replies
View Related
Jul 23, 2013
my problem is naming the function larger() with "int". At least that is what my compiler is leading me to believe.
Code:
#include <stdio.h>
struct Date{
int month;
int day;
int year;
};
[code]....
View 8 Replies
View Related
Dec 24, 2014
I'm trying to compare two float ranges and it seems to be a hit and miss. I'm trying to use a object origin and dimensions comparing it to another set for collisions.
View 12 Replies
View Related