Testing string against array of char[]

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1460 Views - Last Post: 16 April 2014 - 06:45 AM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Testing string against array of char[]

Posted 03 April 2013 - 01:37 PM

I'm back again!

Okay. So, I have a text file with state names, and state abbreviations, thusly:


ALASKA
AK
ARKANSAS
AR
..and so on.

I have to load the abbreviations ONLY from the file into an array of char[ - (already done and tested).

I have to get a 2 char abbreviation as a string,then test it against the state array to make sure it is a valid abbreviation.
As it stands, my test is never finding an invalid abbreviation..
Here is where I get the input

void getState()
{
	char state[10];
	getString("Please enter the state as a 2 char abbreviation:",state,10);
	printf("State Entered:%s", state);
	validState(state);

}//END GETSTATE



and here is my validState function:
bool validState(char testState[])
{
	bool validState = false;
	toupper(testState[0]);
	toupper(testState[1]);

	while(!validState)
	{
		for(int x = 0; x < 50; x++)
		{
			if(!strcmp(stateTable[x],testState))
			{
				
				validState = true;
				break;
			}
		
		}	
	}//END WHILE

	return validState;
}//END VALIDSTATE


Thanks for any help.. I'm going to keep testing and trying to figure out my flaw

This post has been edited by synlight: 03 April 2013 - 01:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Testing string against array of char[]

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:42 PM

The following is probably not doing what you think it is. Remember strcmp() can return one of three values.
            if(!strcmp(stateTable[x],testState))


Also why the negation??


Jim

This post has been edited by jimblumberg: 03 April 2013 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:42 PM

 if(!strcmp(stateTable[x],testState))



If we don't find a match...

 validState = true;



then report success?

EDIT: doh! not used to C, my bad

This post has been edited by jon.kiparsky: 03 April 2013 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:44 PM

Guys... ok

I'm trying not to freak out because I know you can help..

I just tried to run my program, and it says it can't find the specified file.

I closed Visual Studio, and re-opened it.. same problem. ..

The error says:
Unable to start program, then it lists the file path to the Debug folder..
and says it can;'t find the file..

This post has been edited by synlight: 03 April 2013 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:49 PM

toupper(testState[0]);



This probably doesn't do what you think it does.
(at least, I'd expect it to simply return an uppercase char, not to change testState[0] to be uppercase)
Was This Post Helpful? 0
  • +
  • -

#6 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:55 PM

I tried copying the text from the .cpp into a new solution.. it's doing the same thing.

Can anyone help?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:56 PM

Quote

I just tried to run my program, and it says it can't find the specified file.

What specified file? Post the complete error message.

Note: If your program doesn't compile without errors, you can't run the program. Do you get any compile errors?

Jim
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7883
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:57 PM

Visual Studio is not something I use, but you might look for an option to "build clean". This generally wipes out any artifacts of compilation and starts over from scratch. Sometimes helps.
Was This Post Helpful? 0
  • +
  • -

#9 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:58 PM

View Postjimblumberg, on 03 April 2013 - 03:56 PM, said:

Quote

I just tried to run my program, and it says it can't find the specified file.

What specified file? Post the complete error message.

Note: If your program doesn't compile without errors, you can't run the program. Do you get any compile errors?

Jim

No, the program is error free.

The error is a popup window.. do I just take a screenshot and post the image?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Testing string against array of char[]

Posted 03 April 2013 - 01:59 PM

Quote

The error is a popup window.. do I just take a screenshot and post the image?


Give it a try.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 02:01 PM

Posted Image
Was This Post Helpful? 0
  • +
  • -

#12 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 02:10 PM

Here is the Build Output:

1>------ Build started: Project: A4_Maxey, Configuration: Debug Win32 ------
1> LINK : C:\Users\ONISYN\Desktop\A4_Maxey\Debug\A4_Maxey.exe not found or not built by the last incremental link; performing full link
1> A4_Maxey.vcxproj -> C:\Users\ONISYN\Desktop\A4_Maxey\Debug\A4_Maxey.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#13 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 02:17 PM

An old solution works. I'm going to comb over my cpp in a text file and see if I hit a button or something..

Okay.. it's working.

I had to send my friend a text file of the .cpp, have her compile it on her computer, then send me back the zipped solution. Holy hell. I have no idea what happened..

Okay.. my chest hurts LOL.

This post has been edited by synlight: 03 April 2013 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Testing string against array of char[]

Posted 03 April 2013 - 02:39 PM

If you look closely, when you're trying to run it the path it is trying to use is

C:\Users\ONISYN\Desktop\Maxey_4\Debug\Maxey_4.exe

The build, however, is going elsewhere:

C:\Users\ONISYN\Desktop\A4_Maxey\Debug\A4_Maxey.exe

Somehow an A4 got in there in a couple of places?
Was This Post Helpful? 0
  • +
  • -

#15 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Testing string against array of char[]

Posted 03 April 2013 - 02:40 PM

View PostJackOfAllTrades, on 03 April 2013 - 04:39 PM, said:

If you look closely, when you're trying to run it the path it is trying to use is

C:\Users\ONISYN\Desktop\Maxey_4\Debug\Maxey_4.exe

The build, however, is going elsewhere:

C:\Users\ONISYN\Desktop\A4_Maxey\Debug\A4_Maxey.exe


Oh that was from me frantically opening new solutions and pasting in the code as plain text LOL. The screenshot was from one solution, the Build Output was from another one.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3