Okay first post here, and I'm running into a problem. What I would like to do is read in files and have the user enter in the measurement they're looking for. This is what I have so far.

// switch.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int exit=0, previous=0, previous2=0;
	char item='1', x[500], nest='1';
	
do
{

	printf("Select what you would like to search for first\n\n1--Resistors\n\n2--Capacitors\n\n3--Connectors\n\n4--Other\n\n5--Quit\n\n\n");
	scanf("%c", &item);	
	fflush(stdin);

      
	switch(item)
	{
		
		case '1':
			do {
						printf("\tWhat range of Resistors are you looking for?\n\n\tA--1 ohm - 590 ohm\n\n\tB--620 ohm - 59.0K ohm\n\n\tC--60.4K ohm - 1.00M ohm\n\n\tD--Return\n\n");
						scanf("%c", &nest);
						fflush(stdin);
						
						switch(nest)
						{

							case 'A':
							case 'a':
								printf("\nResistors\n");
								fflush(stdin);
								FILE *resistors;

								resistors=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\resistors.txt", "r");
								if(resistors==NULL)
								{
									printf("error opening file\n");
	
									return 0;
								}
								
									else
									{
										printf("\n");
										while(fgets(x, 500, resistors)!=NULL)
										{ 
												
											char delims[] = ",";
											char *result = NULL;
											result = strtok( x, delims );
											while( result != NULL ) {
											printf( "result is \"%s\"\n", result );
											result = strtok( NULL, delims );
												

												fgets(resistors, sizeof(resistors), stdin);
												printf("\nEnter the string sought (less than 40 characters ):\n");
												fgets(x, sizeof(x), stdin);
												resistors[strlen(resistors)-1] = '\0';
												x[strlen(x)-1] = '\0';

												int i;
												for(i = 0 ; (resistors[i] = toupper(resistors[i])) ; i++);
												for(i = 0 ; (x[i] = toupper(x[i])) ; i++);

												printf("\nThe second string %s found in the first.",((strstr(resistors, substring) == NULL) ? "was not" : "was"));

										
												}

											
											printf("%s", x);
										break;
									}
										printf("\n");	
									fclose(resistors);		
									}
								
								break;
									
							case 'B':
							case 'b':
								printf("\n620 ohm - 59.0K ohm Resistors\n");
								fflush(stdin);
								FILE *resistors2;

								resistors2=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\resistors2.txt", "r");
								if(resistors2==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								
									else
									{
										printf("\n");
										while(fgets(x, 500, resistors2)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										break;
									}
										printf("\n");	
								fclose(resistors2);		
						}
								
								break;

							case 'C':
							case 'c':
								printf("\n60.4K ohm - 1.00M Resistors\n");
								fflush(stdin);
								FILE *resistors3;

								resistors3=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\resistors3.txt", "r");
								if(resistors3==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								
									else
									{
										printf("\n");
										while(fgets(x, 500, resistors3)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										
										break;
									}
										printf("\n");	
								fclose(resistors3);		
						}
								
								break;
						
							case 'D':
							case 'd':
								previous=1;
								break;
						}//end nested
					}while(previous!=1);
									break;

			

										case '2':
		do {
					printf("\tWhich type of unit are you looking for\n\n\tA--Picofarad\n\n\tB--Nanofarad\n\n\tC--Microfarad\n\n\tD--Return\n\n");
						scanf("%c", &nest);
						fflush(stdin);
						
						switch(nest)
						{
							case 'A':
							case 'a':
								printf("\nPicofarad\n");
								fflush(stdin);
								FILE *capacitors;

								capacitors=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\capacitors.txt", "r");
								if(capacitors==NULL)
								{
									printf("error opening file\n");
	
									return 0;
								}
									
								
									else
									{
										printf("\n");
										while(fgets(x, 500, capacitors)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										break;
										}
										printf("\n");	
								fclose(capacitors);		
									}
										break;
							case 'B':
							case 'b':
								printf("\nNanofarad\n");
								fflush(stdin);
								FILE *capacitors2;

								capacitors2=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\capacitors2.txt", "r");
								if(capacitors2==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								else
								{
										printf("\n");
									while(fgets(x, 500, capacitors2)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										
										break;
									}
										printf("\n");	
								fclose(capacitors2);		
						}
								
								break;

							case 'C':
							case 'c':
								printf("\nMicrofarad\n");
								fflush(stdin);
								FILE *capacitors3;

								capacitors3=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\capacitors3.txt", "r");
								if(capacitors3==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								else
									{
										printf("\n");
										while(fgets(x, 500, capacitors3)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										break;
									}
										printf("\n");	
								fclose(capacitors3);		
						}
								
								break;
						
							case 'D':
							case 'd':
								previous2=1;
								break;
						}//end nested
			}while(previous2!=1);
			break;
	
	
	case '3':

		FILE *connectors;

								connectors=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\connectors.txt", "r");
								if(connectors==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								else
								{
										printf("\n");
									while(fgets(x, 500, connectors)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										break;
									}
										printf("\n");	
										fclose(connectors);		
								}
								
								break;
						
							case 'D':
							case 'd':
								previous2=1;
								break;
	case '4':

		FILE *other;

								other=fopen("C:\\Documents and Settings\\Duncan-WD\\Desktop\\other.txt", "r");
								if(other==NULL)
								{
									printf("error opening file\n");
								
									return 0;
								}
								else
									{
										printf("\n");
										while(fgets(x, 500, other)!=NULL)
										{ 
												
												char delims[] = ",";
												char *result = NULL;
												result = strtok( x, delims );
												while( result != NULL ) {
													printf( "result is \"%s\"\n", result );
													result = strtok( NULL, delims );
												}

											
											printf("%s", x);
										
										break;
										}
										printf("\n");	
								fclose(other);		
									}
								
								break;
						
														

			case '5':
				exit=1;

			break;
				
		}//end switch

		}while(exit!=1);
			
			
		return 0;
		}

Recommended Answers

All 6 Replies

well that came out pretty weird.

This is a C forum.

fflush(stdin) is BAD. Google it for reasons why.

You say "I have a problem", without providing details of what the problem is.

Go easy on the TAB key ;-)

Dear Wesduncan,

1)Your indentation is relly bad.No one will look into your code.Arrange your code properly.
2)Before posting your code describe your problem verbaly along with the expected result and the result you are getting.
3)If possible try to post the required part of the code.No need to mention full code without any explanation.

GUD LUCK,
DP

sorry about that. the main thing I need to know is how could I have a user enter a measurement and have the program search through a file for a matching string. and then have that print out.

sorry about that. the main thing I need to know is how could I have a user enter a measurement and have the program search through a file for a matching string. and then have that print out.

You need to be more percise than that. Give us an example of the program running.

1) Accept input from the user
2) Open the file
3) Read a line
4) Use strcmp() to see if the measurement is at the beginning of the line
5a) If not, back to 3
5b) If so, close the file and continue.

Also, check out these links:
Formatting C / C++ code
fflush(stdin)
scanf() series

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.