C++ :: Unable To Compare A String With A Char Pointer
Jan 28, 2013
Ok, I'm having a few problems with strings, mostly string functions saying they're not able to compare a string with a char pointer.
int main()
{
int counter = 0;
int x, y, z;
[Code].....
My goal is to take in a command and store it in a string. Different commands have different amounts of information I need. One command is "new flight <flightnumber> <seats available>". First, I try to understand which command is being asked, using the first character to decide. Once I can assume which command is being attempted, I try to separate the string into smaller strings (words) using strtok. I then compare the words that should be constant (new and flight), to make sure the command is syntactically correct (which I use strcmp for). Then I'll go on to create a new flight, which is a class that takes in a char * and integer (which is why I need to convert a char * to integer).
View 2 Replies
ADVERTISEMENT
Sep 19, 2013
I understand you can do
char* charpointer[2];
charpointer = "12";
if (charpointer[0] == '1'){
}
but how can we test for a range? 0-1? so I can compare it to '12'
I wouldn't want to do charpointer[0] == '1' && charpointer[1] == '2' though.
View 1 Replies
View Related
Mar 2, 2013
Why I cannot do this to display each element of my char string? What is the proper way?
Code:
char str[SIZE];
int i;
for(i=0; i<SIZE; i++)
{
printf("%s", str[i]);
}
View 4 Replies
View Related
Dec 3, 2014
how can i compare an element of the char array and string with single chsracter also how to compare char array to cpp string
View 3 Replies
View Related
Jun 12, 2013
Is there a more simple method to copy Buf into str? Buf is a binary string.
Code:
void function(string & str) {
int iWholeSize = 512;
char * Buf = new char[iWholeSize];
.... operations on Buf
string s(Buf, Buf + iWholeSize);
str = s;
}
View 5 Replies
View Related
Mar 5, 2013
Here's the code I'm working on:
string* arrayPush(string *array, char **toks){
if(array[sizeofHistory -1].empty()){
//find the next available element
for(int i=0; i < sizeofHistory; i++ ){
[Code] ....
toks is an array of pointers to strings. I need to assign a toks to array[i].
View 10 Replies
View Related
Oct 6, 2014
how string literal that works with the cin object?
char * str = "This is a string constant";
Is the str stored the address of the first character of the string literal?
But some books just state that the pointer-to-char (char pointer) stores the address of the string literal". So just wonder how it is.
When it is used with cout, cout just treats it like a string and instead of printing the address, it just prints out all characters one by one until it reaches the terminated null character.
If this is the case, then I am just wondering how cin works with it? with a statement like this cin >> str; ?
Does the computer allocate enough memory for it? and then cin stores the first character into the first address and then advances to the next address and stores the next character?
View 1 Replies
View Related
Feb 22, 2013
I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.
private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];
[Code].....
View 9 Replies
View Related
Jun 13, 2013
I have two char* that have the same data in (hypothetically).
std::vector<char*> Buff;
Buff = Split(Line, '.');
char* A = "data", B;
B = Buff.at(0)
Where Split is a function that I made to split a string (Line in this case) into a char* vector, this string contains a line from a file. Line is char* too. The weird problem is when Buff data stored in its 0 position is given to B... because B is equal to A (hypothetically) but when this is compared to do certain functions they doesn't match!
Here an example:
std::vector<char*> Buff;
Buff = Split(Line, '.');
char* A = "map", B;
B = Buff.at(0) // Buff.at(0) should be "map" and is apparently "map"
[Code].....
NOTE: I didn't use switch to compare Cmd because I want it separately for easier debugging.
Is there something wrong with my codes?? or what happened here with those hex values before the string in my variables?
View 6 Replies
View Related
Mar 16, 2014
i have been trying to compare a date format from SYSTEMTIME and a date from a text file(string).But its not working. I tried to change both to string(using osstringstream),char* and int(using sscanf) to do the comparison but with no luck. its pretty simple all i want to do is get the current system date and compare it with the date from the text file. Below is my code:
char szcurrentDate[MAX_PATH] = "";
char szdate_time[MAX_PATH];
SYSTEMTIME st;
GetLocalTime (&st);
GetDateFormat(LOCALE_USER_DEFAULT,NULL,&st,"yyyy-M-d ",szcurrentDate,MAX_PATH); //current system date
//std::ostringstream mm;
[code].....
note : i tried displaying just szcurrentDate and szdate_time they show the date exactly the same. in string,char* or int formats.
View 2 Replies
View Related
May 5, 2013
Code:
#include <stdio.h>
char * strcpy(char *restrict s1, const char *restrict s2);
struct item {
char title[20];
struct item *next;
} firstCard;
int main (){
strcpy(firstCard.next->title, "whatever");
}
I am unable to set firstCard.next->title
View 2 Replies
View Related
May 12, 2012
I am unable to get "reference to a pointer".
Here:
char*p = "hello";
char*&k = p;
while creating reference to a pointer why we have to write *&k , why not &*k?
View 6 Replies
View Related
Jul 14, 2014
I have problem with string compare. I want to compare the string user input with a string in binary. And I don't know how to do it. Problem in function login();Here is the code: And you also can download file in attachment too..
Code:
#include<conio.h>#include<dos.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
char nsb=1;
char ch, password[20], passlogin[20], inputpass[20], checked[20];
FILE *fp;
}
[code]....
View 1 Replies
View Related
Mar 26, 2014
Ok, so I'm writing this code and when I build it keeps saying cannot implicitely convert type int to string even though I declared my variables as string. Why is it giving me this error?
private static string Repair()
{
string result="";
string beep;
string spin;
Console.WriteLine("Does your computer beep on startup?:(y,n)");
[Code]...
View 3 Replies
View Related
Feb 25, 2015
i'm trying to compare a string to a certain string in an array, my code is as follows:
#include <iostream>
using namespace std;
int tu;
int nu=0;
[Code].....
View 5 Replies
View Related
Oct 31, 2014
I am trying to get a list of queries to work right now. I have it so that the user will input 1 of 5 things to compare twin primes in a list. I am just trying to get the queries working fully before actually determining the twin primes that need to be output. The console input will be something like "<= 61" and will display all twin primes from 3 to 61, inclusive.
Well, I thought I had it 100%... Then I realized I have a problem, though. So, I'm reading the string in until a space so I can read in the number after that. So that works fine, but then I also want the "all" and "random" console inputs to work, but they won't unless I put a space after them, which is what I'm trying to avoid...
So, I tried just simply asking for another console input after all of my if statements for the string compare after the first console input. That obviously doesn't work either, though, because it still only wants the first "getline" where it reads until the whitespace.
How to be able to do both on the same console input... The relevant code is posted below:
cout << endl << "What do you want to know about your list of twin primes?" << endl;
string qprompt1;
string qprompt2;
getline(cin, qprompt1, ' ');
string comp1 = ">";
string comp2 = ">=";
[Code] .....
View 5 Replies
View Related
Mar 20, 2013
I have this code :
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
[Code] ...
And this error occued with me I don't know why ???
myHeader.h:42:19: error: expected ")" before string constant
This code is compiled from file called myHeader.h ...
View 2 Replies
View Related
Mar 5, 2014
I'm trying to read a string and then compare it to a word.
My code:
char* cmd = "start";
std::cin >> cmd;
if (strcmp (cmd, "eval"))
eval ();
When the program gets to the cin part, it breaks.
View 4 Replies
View Related
May 9, 2014
I would like to compare two strings, one of which is an independently stored string - the other being a string within a vector. However, i do not know how to do this, as
string thisString = "yes";
vector<string> stringHere;
stringHere.push_back("no");
if (thisString == stringHere[0]) {
cout << "It worked." << endll;
}
does not appear to work.
View 2 Replies
View Related
May 5, 2014
I am trying to compare a string that i have entered with a set of strings that have already been stored in a file. I am using strcmp function but i am not getting the result.
Code:
printf("
Enter string:");
scanf("%s",&m);
ptr_file =fopen("abc.text","r");
[Code] .....
View 10 Replies
View Related
Nov 30, 2013
I am working on a String compare program and i got the program to work properly i was just wondering how to put user input into quotes.
here is my programming
int main {
const int LENGTH = 40;
char s1[LENGTH], s2[LENGTH];
cout << "== String Compare ==" << endl;
cout << "Enter a word" << endl;
[Code] ....
the input is :
Dorito
dorito
the output needs to look like this:
"Dorito" comes before "dorito".
View 2 Replies
View Related
Jun 26, 2014
How do I compare a string vector against an enumerated type? My code so far is:
void convertToLastFirst(vector<string>&names){
enum NameFormat{FIRST, LAST};
for (size_t i = 0; i < names.size(); i ++){
if (names[i] FIRST){
names[i] = LAST;
}
}
}
View 1 Replies
View Related
Nov 25, 2014
I'm trying to reverse every even word in a string (not all of it). But I keep getting an empty output.
For example:
The mouse was caught in peg.
The esmou was thgauc in gep.
Underlined words (even words)
#include<iostream>
#include<string>
using namespace std;
int main()
{
string text, result, temp;
int word_count(0);
cout << "Enter a sentence: ";
[Code]...
View 1 Replies
View Related
Feb 18, 2013
Modify your code by adding your own tests to see if your functions work right. Include at least 6 separate tests, of your choosing.
For example, test the compare function with the first parameter as a blank string -- then with the 2nd as a blank -- then both. Test compare with the first string shorter than the second -- then the other way around. Test your copy function with long strings.
I am struggling with how to use the compare function with a parameter as a blank string. I tried leaving the first parameter blank but doing ("",text) but I don't think that is the correct way of doing this.
#include <cstring>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int myStrLen(const char[]); // return length of null-terminated string stored in char array
[Code] ....
View 2 Replies
View Related
Nov 10, 2014
I'm VERY new in programming and I'm having trouble converting an integer to string. I need to create a function for a programme I'm working on for my school. My problem is that i am only allowed to use the libraries stdio.h, time.h and stdlib.h as well as printf, scanf, system, srand, time and rand. If I was allowed to use itoa or pointers it would be easier but i am not.
View 4 Replies
View Related
Feb 11, 2014
I am trying to read a tab delimited file containing 8 columns and store each element of the column as an 1-dimensional array. Though the program prints the strings correctly as in the commented printf but it doesn't give me first_col[0] value and so on. My code is as follows:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
FILE *int_file1;
int BUF = 1024;
[Code]...
The inputfile test.txt has the following elements:
34932 13854 13854 2012-01-07
172098 49418 53269 2012-01-07
-
-
-
View 7 Replies
View Related