loop not ending

trying to create a program as follows: Next weekend the school is hosting a fencing tournament. An important piece of information they need is the number of fencers registered for each weapon: sabre, epee, and foil. This information is needed as soon as registration closes so that the tournament can begin. With each weapon the fencers can have a ranking of: A, B, C, D, E, or U. They need to know how many fencers of each ranking are registered for each weapon. Each fencer will enter the following information: first name, last name, ranking, and fencing club. When registration is complete a member of the bout committee will enter END for the first name. END is the sentinel that signals that the input data is finished. After the last entry has been made display how many fencers of each ranking in each weapon have registered for the tournament.

The loop is not ending when I type END under the first name. The cout at the end is just testing before I go any further. Am I going about this the wrong way?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        #include <iostream>
	#include <fstream>

		int main()
	{
        char fName[25] = {};
        char lName[25] = {};
        char rank[25] = {};
        char club[10] = {};
        const char sabre[25] = "sabre";
        const char foil[25] = "foil";
        const char epee[25] = "epee";
        int sabreA= 0;
        int sabreB= 0;
        int sabreC= 0;
        int sabreD= 0;
        int sabreE= 0;
        int sabreU= 0;
        
    	      while(fName != "END")
              {  
              std::cout << "First Name? " 
              << std::flush;
              std::cin >> fName;
              std::cout << "Last Name? " 
             << std::flush;
              std::cin >> lName; 
              std::cout << "Ranking? " 
             << std::flush;
              std::cin >> rank;
              std::cout << "Fencing Club? " 
             << std::flush;
              std::cin >> weapon;
              
              if((rank == "A") && (club == "sabre"))
              sabreA++;
              }
             std::cout << sabreA << std::endl;
    
	   return (0);
}
Last edited on
Well, the loop won't end until you input the END, and then go through another loop (since it won't check the while loop until it gets back to the start)...although it could be because your "first name" variable will have 22 extra blank characters. I would suggest using C++ std::string instead of char*, since you are already using std::cout / cin.
I would also suggest using std::strings since they have overloaded the == etc. operators for comparison. For char* use one of the C strcmp() functions.
Ok well I've got it where the loop is ending properly now. I had thought about the variable length myself at first but quickly discounted that because I have written other code where that had not affected it. The biggest problem was that the while loop had to come after the fName cin. After I figured that out it worked right away. The only problem now is the final output it seems like it has to do with my strcmp for each "rank" and "club". I know i'm a total newbie...taking an internet course doesn't give you easy access to ask your instructor these questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

	#include <fstream>
    #include <cstring>
    
	int main()
	{
        char fName[4] ={};
		char lName[25] = {};
		char rank[25] = {};
		char club[10] = {};
        int sabreA= 0;
        int sabreB= 0;
        int sabreC= 0;
        int sabreD= 0;
        int sabreE= 0;
        int sabreU= 0;
        int foilA= 0;
        int foilB= 0;
        int foilC= 0;
        int foilD= 0;
        int foilE= 0;
        int foilU= 0;
        int epeeA= 0;
        int epeeB= 0;
        int epeeC= 0;
        int epeeD= 0;
        int epeeE= 0;
        int epeeU= 0;
        
              std::cout << "First Name? <type 'END' to quit> " << std::flush;
              std::cin >> fName;
              while(strcmp(fName, "END"))              
              {
              std::cout << "Last Name? " << std::flush;
              std::cin >> lName; 
              std::cout << "Ranking? " << std::flush;
              std::cin >> rank;
              std::cout << "Fencing Club? " << std::flush;
              std::cin >> club;
              if(strcmp(rank, "A")&&(club, "sabre"))
              sabreA++;
              if(strcmp(rank, "B")&&(club, "sabre"))
              sabreB++;
              if(strcmp(rank, "C")&&(club, "sabre"))
              sabreC++;
              if(strcmp(rank, "D")&&(club, "sabre"))
              sabreD++;
              if(strcmp(rank, "E")&&(club, "sabre"))
              sabreE++;
              if(strcmp(rank, "U")&&(club, "sabre"))
              sabreU++;
              if(strcmp(rank, "A")&&(club, "foil"))
              foilA++;
              if(strcmp(rank, "B")&&(club, "foil"))
              foilB++;
              if(strcmp(rank, "C")&&(club, "foil"))
              foilC++;
              if(strcmp(rank, "D")&&(club, "foil"))
              foilD++;
              if(strcmp(rank, "E")&&(club, "foil"))
              foilE++;
              if(strcmp(rank, "U")&&(club, "foil"))
              foilU++;
              if(strcmp(rank, "A")&&(club, "epee"))
              epeeA++;
              if(strcmp(rank, "B")&&(club, "epee"))
              epeeB++;
              if(strcmp(rank, "C")&&(club, "epee"))
              epeeC++;
              if(strcmp(rank, "D")&&(club, "epee"))
              epeeD++;
              if(strcmp(rank, "E")&&(club, "epee"))
              epeeE++;
              if(strcmp(rank, "U")&&(club, "epee"))
              epeeU++;
              std::cout << "First Name? <type 'END' to quit> " << std::flush;
              std::cin >> fName;

              }
              
std::cout << "There are " << sabreA << "people competing in rank A with the sabre." <<std::endl;
std::cout << "There are " << sabreB << "people competing in rank B with the sabre." <<std::endl;
std::cout << "There are " << sabreC << "people competing in rank C with the sabre." <<std::endl;
std::cout << "There are " << sabreD << "people competing in rank D with the sabre." <<std::endl;
std::cout << "There are " << sabreE << "people competing in rank E with the sabre." <<std::endl;
std::cout << "There are " << sabreU << "people competing in rank U with the sabre." <<std::endl;
std::cout << "There are " << foilA << "people competing in rank A with the foil." <<std::endl;
std::cout << "There are " << foilB << "people competing in rank B with the foil." <<std::endl;
std::cout << "There are " << foilC << "people competing in rank C with the foil." <<std::endl;
std::cout << "There are " << foilD << "people competing in rank D with the foil." <<std::endl;
std::cout << "There are " << foilE << "people competing in rank E with the foil." <<std::endl;
std::cout << "There are " << foilU << "people competing in rank U with the foil." <<std::endl;
std::cout << "There are " << epeeA << "people competing in rank A with the epee." <<std::endl;
std::cout << "There are " << epeeB << "people competing in rank B with the epee." <<std::endl;
std::cout << "There are " << epeeC << "people competing in rank C with the epee." <<std::endl;
std::cout << "There are " << epeeD << "people competing in rank D with the epee." <<std::endl;
std::cout << "There are " << epeeE << "people competing in rank E with the epee." <<std::endl;
std::cout << "There are " << epeeU << "people competing in rank U with the e[ee." <<std::endl;	
       return (0);
}
Your final output has something to do with strcmp? What does that mean?

If your using multiple if's you should use else if for all subsequent if's after the first one.
All of your lines like

 
if( strcmp(rank,"D")&&(club, "epee"))


make no sense.

The expression (club, "epee") evaluates to club which will always be true. You meant strcmp(club,"epee")?
I don't see why you're using strcmp for comparing string and text. I would use simple
if(rank=="A"&&club=="sabre"){sabreA++;}.
Last edited on
It would need to be

1
2
if( rank[ 0 ] == 'A' && !strcmp( club, "sabre" ) )
    ++sabreA;


in that case.
Topic archived. No new replies allowed.