[cpp] sumthing wrong with comparing

Custom Title Activated
Loyal Member
Joined
Nov 28, 2004
Messages
2,236
Reaction score
0
Code:
// Namu_Darbas_01.cpp
//

#include "stdafx.h" //MS STUDIO 2005
#include "stdio.h"
#include "math.h"

int main()
{
	printf("FOR AR WHILE to be used ('for' or 'while')?\n");
	
	char choosed[10];
	scanf("%s",&choosed);
	printf("INPUT: %s \n",choosed);

	if (choosed == "for")
	{
		printf("BUS NAUDOJAMASI FOR\n");
		double x,y;
		for (x=0.1;x<=1.5;x+=0.07)
		{
			y=sin(x);
			printf("NUMB: %f \n",y);
		}
		
	}
	else
	{
		printf("BUS NAUDOJAMASI WHILE\n");
		double x,y;
		
		x=0.1;
		while (x<=1.5)
		{
			y=sin(x);
			printf("NUMB: %f \n",y);
			x+=0.07;
		}
	}



	

	int temp1;
	scanf("temp1");

	getchar();

	return 0;
}

I had started to program on C and encountered quite dumb logical error and I don't see where's the problem.

Why the hell in the
Code:
if (choosed == "for")
bool is 0? the expression is true, isn't it??

previously I have tried to use INT for asking question what method will be used (1 for FOR and 2 for WHILE) and it was ok on comparing.
 
Solution

Here ya go lex, its tedius but hey it works...

First! - add an include for string.h

then, edit your if statment to be
Code:
if (strcmp(choosed,"for")==0 )
Code:
Here are the comparing values you would use:
"<0" string1 < string2
"0" string1 = string2
">0" string1 > string2

tried it and it works. (This is how I always compare my strings.)

(all of your code edited)
Code:
// Namu_Darbas_01.cpp
//
#include "stdafx.h" //MS STUDIO 2005
#include "stdio.h"
#include "math.h"
#include "string.h"

int main()
{
	printf("FOR AR WHILE to be used ('for' or 'while')?\n");
	
	char choosed[10];
	scanf("%s",&choosed);
	printf("INPUT: %s \n",choosed);

	if (strcmp(choosed,"for")==0 )
	{
		printf("BUS NAUDOJAMASI FOR\n");
		double x,y;
		for (x=0.1;x<=1.5;x+=0.07)
		{
			y=sin(x);
			printf("NUMB: %f \n",y);
		}
		
	}
	else
	{
		printf("BUS NAUDOJAMASI WHILE\n");
		double x,y;
		
		x=0.1;
		while (x<=1.5)
		{
			y=sin(x);
			printf("NUMB: %f \n",y);
			x+=0.07;
		}
	}



	

	int temp1;
	scanf("temp1");

	getchar();

	return 0;
}

(Save yourself some trouble and just tell the user to input an int... -.-)
 
Last edited:
Somewhat, its like java, see with java to compare strings you would have to go string1.equals(); (Since in java a string is an object)

Using strcmp(); isnt to bad thoe.
 
ssddss said:
Somewhat, its like java, see with java to compare strings you would have to go string1.equals(); (Since in java a string is an object)

Using strcmp(); isnt to bad thoe.
I hope my lecturer wouln't carp at this.

OK, it seems my first homework is done.
Code:
// Namu_Darbas_01.cpp
//
#include "stdafx.h" //MS STUDIO 2005
#include "stdio.h"
#include "math.h"
#include "string.h" // comparing with "strcmp"

int main()
{
	//PROMPTING FOR METHOD
	printf("FOR ar WHILE naudosim ('for' or 'while')?\n");
	char choosed[10];
	scanf("%s", &choosed);

	//COMPARING FOR METHOD
	if ( strcmp(choosed,"for") == 0 )
	{
		//METHOD: "FOR"
		printf("BUS NAUDOJAMASI FOR\n");
		
		double x,x_prad,x_gal,x_reiksmiu_skaicius,x_zingsnins,y;
		x_prad = 0.1;
		x_gal = 1.5;
		x_reiksmiu_skaicius = 20;
		x_zingsnins = ( (x_gal - x_prad) / x_reiksmiu_skaicius);
		int eilutes_nr;
		eilutes_nr = 1;

		for (x = x_prad; x <= x_gal; x += x_zingsnins)
		{
			y = ( ( pow(sinh(x),2) - 18 * pow(x,2) ) + ( 12 * pow(x,3) - pow(cosh(x),2) ) - (pow(x,4) / 4*3*2*1) + (pow(x,5) / 5*4*3*2*1) );
			printf("%2d:\tKAI X=%f Y=%f \n",eilutes_nr,x,y);
			eilutes_nr += 1;
		}
		
	}
	else
	{
		//METHOD: "WHILE"
		printf("BUS NAUDOJAMASI WHILE\n");

		double x,x_prad,x_zingsnins,x_reiksmiu_skaicius,y;
		x_prad = 0.2;
		x_zingsnins = 0.08;
		x_reiksmiu_skaicius = 18;
		int eilutes_nr;
		eilutes_nr = 1;
		
		x = x_prad;
		while (eilutes_nr<=x_reiksmiu_skaicius)
		{
			y = ( ( pow(sinh(x),2) - 18 * pow(x,2) ) + ( 12 * pow(x,3) - pow(cosh(x),2) ) - (pow(x,4) / 4*3*2*1) + (pow(x,5) / 5*4*3*2*1) );
			printf("%2d:\tKAI X=%f Y=%f \n",eilutes_nr,x,y);
			
			x += x_zingsnins;
			eilutes_nr += 1;
		}
	}


	//for pause	
	getchar();
	getchar();

	return 0;
}
 
[lexx] said:
damn, so Cpp can't compare two strings directly, only with strcmp?


What do you mean by only?

It isn't "only"... C++ is, in my opinion, the most powerful programming language, its portable, used almost everywhere.

Anyway... strcmp, yes... I never tried comparing directly, I have used strcmp in some apps before.

I see you are taking up another intrest, I suggest you get some books on C.

And when in doubt, check the MSDN library, may be expensive to sign up, but you get the library on DVD :angel_hyp


:dulya: :dulya: Bitblaster:dulya: :dulya:
 
trcmp is in fact a work around, implanted in the string library. Point is that contrary to what you might be used to in other languages, especially soft-typed ones as PHP, JavaScript, etc, in C++ the data types have to be set to match manually. You cannot simply compare a char array with a data string, not unless you go by both the char array and the data string one char at the time and compare it. This is exactly what strcmp does.

Soft-type languages do it as well by the way, but they let their compiler do most of the work. This is one of the reasons a PHP script will never be as fast as the same script in C++ -> a lot of time is going into data type verification and transferring.

Ow, and getting a few good books on C++ is a good idea IMHO as well; Just don't be alarmed by the enormous size of the things.. :D
 
I had already experienced the differences between php and cpp. Well, nothing to do; if I need to declare everything I guess I can't grip bout that.

Already got tons of info during my lectures and don't have enought time to go deeper by myself so I hope ppl here will help me on problems if I experience any.
 
Couldnt you try using <string> and define all of your character arrays as strings. If you dont want to use strcmp, or strncmp you could use that header along with std namespacing. Some people prefer using this type of string, either way. just giving you options.

Code:
#include <string>
#include <iostream>
using namespace std;

int main()
{
             	string name;
	string input;

	cin>>input;

	name = "yournamehere";

	if(name==input) {
	//... and so forth
 
The reason using the std namespace string (standard template library(stl) string class) works with == is because the stl string class overloads that operator and causes it to act differently.

Within that overloaded == operator, it does a strcmp call which in turn compares each character within the internal character array that exists within the class.

As stated before, a character array cannot be compared to a character array using the == operator because they are actually a grouping of the base data type char. You could use the == operator on each array offset in the character array, and that would require a loop. Instead of writing loops over and over, every time you need to manipulate a character array, standard C provides the string functions... strcmp, strchr, strrchr, strstr, strrstr, strcat, etc.

There are a whole host of string manipulation functions... here's what I found by checking man pages on a Unix box -- the listing includes short descriptions of what the functions do:

man -k str | grep ^str
strcasecmp(3), strncasecmp(3), strcasecmp_l(3), strncasecmp_l(3) - compare strings, ignoring case
strcat(3) - concatenate strings
strchr(3), strrchr(3) - locate character in string
strcmp(3), strncmp(3) - compare strings
strcoll(3), strcoll_l(3) - compare strings according to current collation
strcpy(3), strncpy(3) - copy strings
strcspn(3) - span the complement of a string
strdup(3) - save a copy of a string
strfmon(3), strfmon_l(3) - convert monetary value to string
strftime(3), strftime_l(3) - format date and time
strict(3pm) - Perl pragma to restrict unsafe constructs
string(ntcl) - Manipulate strings
stringlist(3), sl_init(3), sl_add(3), sl_free(3), sl_find(3) - stringlist manipulation functions
strlcpy(3), strlcat(3) - size-bounded string copying and concatenation
strlen(3) - find length of string
strmode(3) - convert inode status information into a symbolic string
strpbrk(3) - locate multiple characters in string
strptime(3), strptime_l(3) - parse date and time string
strrchr(3) - locate character in string
strsep(3) - separate strings
strspn(3) - span a string
strstr(3), strcasestr(3), strnstr(3), strcasestr_l(3) - locate a substring in a string
strtod(3), strtof(3), strtold(3) - convert ASCII string to floating point
strtod_l(3), strtof_l(3), strtold_l(3) - convert ASCII string to floating point
strtok(3), strtok_r(3) - string tokens
strtol(3), strtoll(3), strtoimax(3), strtoq(3) - convert a string value to a long, long long, intmax_t or quad_t integer
strtol_l(3), strtoll_l(3), strtoimax_l(3), strtoq_l(3), strtoul_l(3), strtoull_l(3), strtoumax_l(3), strtouq_l(3) - convert a string value to a long, long long, intmax_t, quad_t unsigned long, unsigned long long, uintmax_t, or u_quad_t integer
strtoul(3), strtoull(3), strtoumax(3), strtouq(3) - convert a string to an unsigned long, unsigned long long, uintmax_t, or u_quad_t integer

Some of them have very similar functionality... like strcpy will copy a string and strdup will duplicate a string... the difference? strcpy places the copy into memory that's already defined in your program. strdup will allocate new memory and place the string copy into it.

I suggest being familiar with the C character array type string, but also taking advantage of the bonuses that the stl string class can give you ... you can always get a C character array from the stl string class by calling the c_str() method.
 
Back