C++ :: Comparing Two String In If Statement
Aug 31, 2014
i actually i want store string in r and try to compare other string (room_no) in function check but i try many time it still having error
#include<iostream>
#include<conio.h>
#include<fstream>
[Code].....
View 2 Replies
ADVERTISEMENT
Jul 1, 2014
I am trying to do this but it can never seem to work.
#include <iostream>
using namespace std;
int main () {
char charOne;
cin.get(charOne);
cin.ignore(1000, '
[Code] .....
Basically i want to keep looping until i enter an a or A (also does this apply to if statements as well?)
View 6 Replies
View Related
Oct 25, 2014
The issue that I'm having is that when I run this part of the code it seems to enter the if statement and set it to 0 when comparing the array to the input.
What I want it to do is to search the array for the grade, set it to zero, and then exit the loop in case there are any repeat grades. It always sets the first number to 0 and then exits.
//Below is the prototyping for the array
int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};
//There is some code between here, but it is probably not relevant so I didn't post it.
printf("Enter the grade that you want to remove. Press Enter after each input.");
printf("Enter 0 to exit: ");
[Code] ....
I didn't mean to leave the "prototyping" part in the code. I was initially going to put the function prototypes in there, but decided it wasn't necessary for the question.
View 6 Replies
View Related
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
Feb 7, 2013
I am stuck in this program, Be given a string of chars, where each single char belongs to the following alphabet: a..zA..Z0..9 (So, in the string there are only lowercases, uppercases and digits. No blank, no comma, ...). For every char of the given alphabet, count how many times in the string
1-- the char belong to a sequence of identical chars whose length is at least three (i.e.: in the string cc74uyrpfccc348fhsjcccc3848djccccc484jd for three times the character 'c' satisfies this condition)
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
[Code]...
2-what is the longest substring of characters strictly rising interm of ASCII code(the following is greater (>) of the previous)
3- what is the longest substring of successive characters interm of given string rannge (i.e.: fhkjshdfruytyzABCDEfglsj => 7)
View 1 Replies
View Related
Dec 15, 2013
I am wondering if there is a way to compare two strings that aren't identical with each other.
instance:
str1="ABC123";
str2="A****3";
if(str1==str2) {
cout << "It's a match";
}
View 5 Replies
View Related
Mar 26, 2014
My program will scan a message sent over Steam (An online game distributor ) and I want it to check if the Message contains one of the elements in the array. I want it to compare the String of the message to the values in the String array. I have tried looking into it but have unfortunately found nothing that would go along with my program outline. Here is my code so far:
if (add == true) {
string[] itemNames = { "keys", "tickets", "nametags", "descriptiontags" };
string[] splitmessage = message.Split(' ');
foreach (string word in splitmessage) {
int strNumber;
[Code] .....
I am hoping for this to look like the picture of the code I wrote in the Attachments. [URL] .....
View 10 Replies
View Related
May 20, 2014
I'm writing a code generator that produces a function from the strings to the ints. I'll be using the generated code as a "from string to enum" utility. For example:
Code: enum Color {
Red, Green, Blue, Banana
};
// The definition of colorFromString is generated somewhere.
Color colorFromString(const std::string & s);
[Code].....
The implementation of the generated code is a trie. I've seen implementations in the past (including the one at work that I'd like to replace).
Anyway, say you need to compare the region of the string
Code: const char s[] = "holiday"; from index 3 until before index 6 against the string "ida".
I can see two bits of code that my generator could produce. One is
Code: bool hasIda = std::equal(s + 3, s + 6, "ida"); and the other is
Code: bool hasIda = s[3] == 'i' && s[4] == 'd' && s[5] == 'a';
The existing code generator uses the latter method, claiming (I think) that the generated instructions are more efficient on some architectures. Is there any way to determine which is better generally, or do I have to examine the assembly produced on all target platforms?
View 4 Replies
View Related
Jun 5, 2014
I am trying to compare strings (char*) using strcmp but I am having a hard time doing it with boost::bind. It compiles but it crashes when I run it.
I have a std::vector<boost::shared_ptr<DeviceInfo>> cMonitoredDevices and one cCurrentDevices. I used a typedef DeviceContainer for std::vector<boost::shared_ptr<DeviceInfo>>.
DeviceInfo is a simply struct that contains a char[128] Name (and other fields not important for this issue) that I want to use to compare.
So I am trying to find the DeviceInfo (based on Name) that are in cMonitoredDevice but not in cCurrentDevices. My problem is retrieving the Name value for the strcmp. Here is what I have so far
for(DeviceContainer::iterator pDevice = m_cMonitoredDevices.begin();
pDevice != m_cMonitoredDevices.end(); pDevice++) {
if (std::find_if(cCurrentDevices.begin(), cCurrentDevices.end(),
boost::bind(&strcmp, boost::bind(&boost::shared_ptr<DeviceInfo>::value_type::Name, _1),
(*pDevice)->Name) == 0) == m_cMonitoredDevices.end()) {
}
}
View 2 Replies
View Related
Oct 2, 2014
I'm looking to take in an array of less than 50 strings, and I want to find all of the unique words in that array (omitting the words that are repeated) and then outputting the unique words and unique word count. My code compiles, but my unique array is couting all of the words contained in the original array regardless of uniqueness.
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
cout << "Please type in some words." << endl;
cout << "Type END and return when you are finished." << endl;
[code].....
This is what I get back.
You typed the following 14 words: red, green, blue, red, red, blue, green, blue, orange, red, reg, apple, banana, banana, END,
You typed the following 0 unique words: red, green, blue, red, red, blue, green, blue, orange, red, reg, apple, banana, banana, END
I'm not worried about the unique count yet, I just want to get the unique array containing the correct strings.
View 3 Replies
View Related
Dec 20, 2013
I am having a slight issue with the strcmp function. I am comparing string data from two different files. I want the function to output a set of information if the strings are the same and a different set of data if the strings are different. My issue is, the function outputs the data that's the same but not different.
I had an else statement that compared the data if it was NOT equal but it only duplicated the data in the file.
One file is a listing of 100 books with 10 lines of information and an assigned market. The second file is a listing of the markets contained in the books file. However, the books file has a market that is not located in the markets file. The "missing" market is what is not priting or displaying.
// final project2 file
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int ARRAY_SIZE = 500;
const int MARKET_ARRAY = 6;
ifstream infile, infile2;
ofstream outfile;
[Code] .....
View 6 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
Oct 10, 2014
The List[] array contains some empty entries but most are filled, yet every time I run this routine the variable Index makes it to the end of the array. I've tried many different routes and can't seem to figure out this simple issue.
string Result = string.Empty;
while ((Index < (List.Length - 1)) || !string.IsNullOrEmpty(Result))
{
Result = List[Index];
Index++;
}
return Result;
View 9 Replies
View Related
Mar 7, 2013
Here is the code:
// Creating and joining string objects
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
// List names and ages
void listnames(string names[], string ages[], size_t count) {
[Code] ....
I may be wrong, but the problem seems to be in the function "listnames". Specifically, the output statement inside the while loop. I don't understand , how the ++ operator is behaving in this statement. The output produced does not match what's printed in the book. I usually just type all the examples, but with this one I also downloaded the source code from the book's website to make sure the error wasn't due to mistyping.
View 4 Replies
View Related
May 3, 2013
I'm working on a CGI application. I'm trying to test my input with a switch statement and output the result with html tags to populate a web page. From within the switch, I've coded as follows:
HTML Code:
switch(mFunc) {
case 0:
cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2 << "</h2>" << endl;
break;
case 1:
cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl;
break;
I know that I'll need to put this in an html body with a content type as such:
HTML Code:
cout << "Content-type: text/html
";
cout << "<html><body>
";
Am I able to do that directly inside of the switch statement?
View 11 Replies
View Related
Sep 7, 2013
How to make if else code below into SWITCH STATEMENT?
cout << "Enter the temperature: ";
cin >> temp;
cout << "Enter the pressure: ";
cin >> pressure;
cout <<endl;
[Code]....
View 6 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
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
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
Sep 2, 2014
If I have 2 arrays, say array1 = {2, 2, 4, 5, 6} which has sorted array elements and array2 = {4, 2, 6, 2, 5} that has same elements as array1 but not in sorted condition. How can I compare that both arrays have same elements. I have written the following code which works in all conditions except when array1 has two same elements and array2 has all different elements.
counter=0;
for (i=0; i<5; i++) {
for (int j=0; j<5; j++)
if (array2[i] == array1[j]) {
counter++;
array1[j]=0;
[Code] ....
View 7 Replies
View Related
Jun 22, 2014
I have trouble comparing two strings with strcmp. The bold part of the code are the parts that are not working and i hope somebody can explain to me what i did wrong. Goal of the code is to compare a city name with the name of already created cities and when two of them match to creat a Street between them. Depending on the street pointer different streets can be created
my vector containing all already created cities (it seem to work)
std::vector<city*> citylist;//all cities
find city using strings and creating a road between them:
bool Map::add_street(Street *street, const string firsttown , const string secondtown) {
city* hilfs1=Map::find_city(erste);
city* hilfs2=Map::find_city( zweite);
if (hilfs1==NULL || hilfs2==NULL)
{return false ;} // Problem :both pointers are always NULL
[Code] ....
Here is also my implementation of the getname function :
std::string city::getname() {
return this->name;
}
View 2 Replies
View Related
Feb 6, 2013
i have 3 arrays total, 2 of them i am comparing to see if any of the values match at all, and then i am putting the value that matches into the 3rd array. currently i am trying
int isthere (int match[], int maxmatchLength,
const int vec1[], int vec1Length,
const int vec2[], int vec2Length)
{
for (i=0; i<vec1Length; i++)
if vec1[i]==vec2[i];
vec1[i] = match[i];
}
But this will just match the same values are in the same spot. how do i fix it so that it compares one value in the first array to the whole second array, before going to the next number.
View 1 Replies
View Related