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;
}