hi.. i would like to compare a user-input string with another string to see if they matched so what should i do?

sample code:

getline(cin,string);
 
if((i==25)&&([I]string comparsion here[/I]))
do this;
else
if((i==25)&&([I]similar string comparison[/I]))
do something else;

so what should i do? i tried

const char* str = string.c_str();
 if((i==25)&&(strcmp(str,"string")))
  do this;
 else
  if((i==12)&&(strcmp(str,"strings")))
   do something else;

but it manages to fufil the first if-condition but couldn't fufil the second if-condition.. thanks

You are writing a c++ program, don't the input string to char* -- just use std::string's == operator (and don't use the variable name "string" because it is the name of a c++ class and will confuse the compiler. I don't know the relevance of i, but that is probably something else in your program.

std::string input_string.
getline(cin,input_string);

if(i == 25  && input_string == "string")
{
     // do something
}
else
if(i == 12 && input_string == "strings")
{
     // do something else
}

>>but it manages to fufil the first if-condition but couldn't fufil the second if-condition.

what are the values of i and input_string? put a print statement somewhere or learn to use your compiler's debugger. Also: note the spelling of "string" and "strings" are not the same in your post. Is that true in your actual program? If it is, maybe that is the reason it never falls into the second condition.

thanks..

if((i==25)&&(input_string=="ca"))
input_string="ba";
else
if((i==25)&&(input_string=="ba"))
ampm="ca";

i've done this but it still can't do the second if condition..

remove the else condition.

if((i==25)&&(input_string=="ca"))
input_string="ba";
//else
if((i==25)&&(input_string=="ba"))
ampm="ca";

ok... i did what you said on my prgram but it still isn't working.. so here's part of the program... i can't figure out what's wrong...

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

void ampmcalculation(string);

void main()
{
int hrs, mins,timeDiff,equihrs;
string ampm;

cout<<"Enter hrs, mins and am/pm of time: ";
cin >>hrs>>mins>>ampm;
cout<<"Enter time difference (in hrs): ";
cin >>timeDiff;
equihrs = hrs+timeDiff;
if(equihrs<12)
{
}
else
{
if(equihrs==12)
{
void ampmcalculation();
}
else
{
void ampmcalculation(string ampm);
equihrs=equihrs-12;
}
}
cout<<"The equivalent time is "<<equihrs<<":"<<setfill('0')<<setw(2)<<mins<<ampm<<".\n";
}

void ampmcalculation(string ampm)
{
if(ampm=="am")
{
ampm="pm";
}
else
{
if(ampm=="pm")
{
ampm="am";
}
}
}

You should put

equihrs=equihrs-12;

inside this if also

if(equihrs==12)

Other than that I dont see any problems

if i put that in the if i will get 0:00am or 0:00pm which is not desirable... the problem that i'm facing now is that i can't switch am to pm and vice versa when equihrs>=12. thanks anyway...

Have you checked your prog. The program is switching between am and pm.
I gave the input as 5 30 am and time difference 9. The o/p I got is 2:30pm. This is infact correct. Then why you are saying its not switching???

The problem I am seeing with your code is
Suppose I gave the input time as 0 0 am and time difference as 12.
The o/p should be 0:00pm but your program o/ps 12:00pm . This is infact wrong. Thats why I suggested to put

[LEFT]equihrs=equihrs-12;[/LEFT]

inside

if(equihrs==12)

it's working?? but when i debug it doesn't switch.. i just tried but its still not working...

if(equihrs<12)
{
}
else
{
if(equihrs==12)
{
void ampmcalculation();
}
else
{
{
void ampmcalculation(string ampm);
equihrs=equihrs-12;
}

The line in RED above is not calling a function, it is just declaring the function. If you want it to actually call that function you have to remove the "void" in front of the function name, add a parameter to the function call in the first red line, and the word "string" from the parameter list.

omg... why is it that i didn't see it? one of the worst mistakes i've made in programming.... ha.. thanks for helping me out... been wodering why it didn't work for a few days and was about to give it up.. thanks....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.