Prototyping advise on conditions

P,s(Was written on a android phone mobile c use (so might look wrong if copy and pasted. )

Hi I'm building a Test like, exam type based program( something like this is better left to the browser with JavaScript.) So i need your input on how i could:

#1_Separate the conditions within the:
If statements, the answers should not request the user to static type each question rather choose the corresponding answers on what order freely(, while it works under a OR condition it however breaks, and the same answer can be repeated to pass.)

#2_Should there be a IF statement that meets OR conditions, or separate it into its own as a Do-while request. No need for switch statements.

#3_Also onditions that meet conditions but do not require the condition to meet every && rather || while not exceeding 7 imputed answers is the end goal.


Any feedback or input is more then welcome.
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
101
102
103
104
105
106
#include <iostream>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <math.h>


///////////////////
using namespace std;
////////////////// 


   void Question_2(){
 	         //235
  cout<<setw(230)<<"Q2: Name 7 hazards: "  <<flush;
  string datatype = "people";
  string datatype_2 = "powerlines";
  string datatype_3 = "slips""and""spills";
  string datatype_4 = "snakes";
  string datatype_5 = "trips""and""falls";
  string datatype_6 = "vermin";
  string datatype_7 = "trees";
   /* 
  string ca = datatype + datatype_2 + datatype_3 + datatype_4;
   cin>>ca;
   */
 
  cin>> datatype>>datatype_2>>datatype_3>>datatype_4>>datatype_5>>datatype_6>>datatype_7;
 	//datatype_2>>datatype_3>>datatype_4;
 
  
 if((datatype=="people")&&(datatype_2=="powerlines")&&(datatype_3=="slips""and""spills")&&(datatype_4=="snakes")&&(datatype_5=="trips""and""falls")&&(datatype_6=="vermin")&&(datatype_7=="trees")){
 
 	
 	cout<<"----------------------------------|"<<endl;
 	cout << setw(35)<< "//^_^)> Pass|" << endl;
 	cout<<"----------------------------------|"<<endl;
     cout<<setw(21)<< setprecision(0) <<sizeof(datatype)
 	<<":bytes|96bits|information"<<endl;
     
  
 	
 	}else if((datatype!="people")&&(datatype_2!="powerlines")&&(datatype_3!="slips""and""spills")&&(datatype_4!="snakes")&&(datatype_5!="trips""and""falls")&&(datatype_6!="vermin")&&(datatype_7!="trees")){

 cout<<"---------------------------------------------|"<<endl;
 	string error  = {"Try again?"};
 	int z = 1;
	int x = 0;
	for(x=0;x<z;x++)
     cout<<setw(116)<<
               setprecision(0)<<"| " << error<<endl;
 	cout << setw(116)<< " Hint, anything with _ this requires        you to finish the sentence. | " << endl;	
 	cout << setw(116)<< " ------| " << endl;	
 	cout << setw(116)<< " people| " << endl;
 	cout << setw(116)<< " ------| " << endl;		
 	cout << setw(116)<< "power_ | " << endl;
 	cout << setw(116)<< " ------| " << endl;
     cout << setw(116)<< "slips_ | " << endl;
 	cout << setw(116)<< " ------| " << endl;
 	cout << setw(116)<< "Sna_   | " << endl;
 	cout << setw(116)<< " trip_ | " << endl;
 	cout << setw(116)<< " Ver_  | " << endl;
 	cout << setw(116)<< "tre_   | " << endl;
 cout<<"---------------------------------------------|"<<endl;
 		 
 	 
 	  return Question_2();
 	}
     else return Question_2();
  }
 
 
 int main ()

  {
   //Call requests
 
 /*  Team_learning();
     Question_1();
 */
     Question_2();
 /*  Question_3();
     Question_4();
     Question_5();
 */
 /* 
     Question_6();
     Question_7();
     Question_8();
     Question_9();
     Question_11();
     Question_12();
     Question_13();
     Question_14();
     Question_15();
     Question_16();
  	Question_17();
  	Question_18();
  	Question_19();
  	Question_20();
    
  */
	return 0;	
	
	}
Last edited on
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
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

int question2();
int askForSecondChance(const std::string& question, int attempts = 1);
void waitForEnter();


int main()
{
   question2();

   waitForEnter();
   return 0;
}


int question2()
{
    std::vector<std::string> q2words { "people", "powerlines", "slips and spills",
                                       "snakes", "trips and falls", "vermin",
                                       "trees" };
    std::cout << "\nQ2: Name 7 hazards: \n";
    int result {static_cast<int>(q2words.size())}, index {};
    for(const auto& a : q2words) {
        std::cout << "hazard n. " << ++index << ": ";
        std::string tmp;
        std::getline(std::cin, tmp);
        if(a != tmp) {
            int chances = askForSecondChance(a);
            if(chances < static_cast<int>(a.length())-1) { result--; }
        } else { 
            std::cout << "Very good!\n"; 
            result--;
        }
    }

    std::cout << "You missed " << result << " answers out of " 
              << q2words.size() << '\n';

   return result;
}


int askForSecondChance(const std::string& question, int attempts)
{
    if(attempts < static_cast<int>(question.length())-1) {
        std::cout << "Ouch! Sorry, wrong answer. Please, try again (" 
                  << question.substr(0, attempts) << "...): ";
        std::string tmp;
        std::getline(std::cin, tmp);
        if(tmp != question) {
            attempts = askForSecondChance(question, ++attempts);
        }
    } else {
        std::cout << "Sorry, you've run out of chances.\n";
    }
    return attempts;
}


void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Awe thanks, um so you added a global, nested prototyping below the main, nice.

Not sure about vector, (May need to research that. Keep thinking floating point vectors)with multi dimensional arrays. So you added casting.

Okay... Hmm and some call request functions based on input. Okay.

(Not sure why std nested on each paragraph rather in the header) SO binery scope recursion operator isn't related to std?
Last edited on
a global, nested prototyping below the main

Hi, Shibitto.
I’m not sure I understand correctly. I added a couple of functions below main() and their prototypes above it, but they are not nested...

why std nested on each paragraph rather in the header

In C++ there are classes. It’s a common practice to use headers for classes declaration, while headers of function prototypes only are few and far between.

SO binery scope recursion operator isn't related to std?

If I used a binery scope recursion operator I did it unintentionally :) since I haven’t got a clue of what it is.
Perhaps he means the scope resolution operator::, that you used to scope items in the std namespace?

The scope resolution operator has several uses depending on content.

It can be used to tell the compiler to use a variable that is hidden by local variable with the same name:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int i = 10;

int main()
{
    int i = 100;

    cout << "i in current scope: " << i << " i from the more global scope: " << ::i << endl;
}


Also it can be used to scope class names:

1
2
3
4
5
6
7
8
Class MyClass
{
   int n1, n2;
   public:
   {
       void func1();
   }
};

public void MyClass::func1() ---Use of Scope Resolution Operator to write
function definition outside class definition
{
   // Function Code
}


It can also be used to scope items hidden in namespaces (any namespaces, not just the std namespace), as shown by Enoizat.





My understanding is c++98, given i had to throw part of my understanding into the pits of hell from c++2011 upwards, but i really like static cast it's awesome, after a week of research and breaking down what i don't know alot of things are not explained but it's understandable as i am a low life foot soldier.

I removed a bit of STD for simplistic reasons at breaking down the templates. Most imply to add a virtual class with some classes.

So I added a class for visual representation but really adds nothing.

So i need to learn static_cast<int>(list.size)
Or why chances = asfc(A)
Or tries = local (V) why?
Any good c++2011 guides to learn, so far I'm still processing everything above just not sure what effects why in great detail like is question its own variable because it's out side of interest question, but has a string address being called

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
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std; //or std::cout<<""<<std::endl; 
//c++2011
int local();//int *ad;//global area has no { data } block
/*
Global functions here(Clsss is a transparent visual aid 
*/
class Questions{
private:
public:
          int local(){ 
	     std::cout<<"list: "<<std::endl;
		//local function
		// block data { data } paranthesis
     	vector<string> list = {"delta", "alpha","quadro",  
         "omega","beta","vega","lexlepot"}; 
	     // vector allocates array lists, and integers^^
	    	int function{
	     //int function nested inside a local scope^^ function being called from main and global
	      //static cast call scopes data nested in parenthesis            //then out puts it into a for loop itterator
           
		static_cast<int>(list.size()) },index{};//index is 0
		for( const auto v : list)//v prints out array
		{
          string b;        //out puts b variable to getline
          getline(cin, b); //getline requests computer input+b
          //  std::cin>>b;
          if(v!=b){/*
cout<<function<<"~~one~~"<<++index<< ""<<list.size()<<"";
 cout<<"~~two~~"<<++index<< ""<<list.size()<<"";
  cout<<"~~three~~"<< ""<<list.size()<<"";
 */cout<<"The number of arrays in the list is: "<<function<<endl;
   std::cout<<"Ordered number is: "<<++index<<std::endl;
   std::cout<<v<<"'s "<<"list size is: "<<list.size()<<std::endl;
	}
         //}else{}//empty block
         }//if condition closing bracket
          return function; //loops or itterates function
         }//local closing bracket
         
         int TryAgain(){
         int tries
         	{};//empty block
         return tries;
        }//try again closing bracket
         
        };//Questions closing bracket
         int main()
         {
	    Questions Global;
         Global.local(); //first read function
	    return 0;
      	};



Why does the code overflow outside of thread post? Oops i now know why(*-*)
Last edited on
Rather than casting size() (type size_t) to int, why not just use size_t throughout?
1
2
            size_t chances = askForSecondChance(a);
            if(chances < a.length()-1) { result--; }
Hi, Shibitto.
I think you’d better avoid using for your variables the same names that are there in the standard namespace, expecially if you introduce it entirely in your program.
I mean, if you write:
using namespace std;
and then you use, for your variables, names like ‘list’ of ‘function’, you’re playing with fire. IMHO.

Here are some hints about classes and member functions (I also included an example of what dhayden suggested you):
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
#include <iostream>
#include <limits>
#include <string>
#include <vector>

class Question {
private:
    int tries {};
public:
    void local();
    bool tryAgain();
};

void Question::local()
{
    std::vector<std::string> mylist = { "delta", "alpha", "quadro",
                                        "omega", "beta",  "vega",
                                        "lexlepot" };
    std::cout << "Guess the words. You can make 3 errors.\n";
    size_t index {};
    for(const auto v : mylist) {    // v becomes a copy of the subsequent element 
                                    // of mylist at every iteration
        std::cout << "Type your word: ";
        std::string b;
        std::getline(std::cin, b); // std::getline requests ENTER
        if(v != b){
            std::cout << "You typed '" << b << "' while '" << v 
                      << "' was expected.\n";
            if(!tryAgain()) { break; }
        }
        std::cout << "Attempt left: " << 3 - tries << " on " 
                  << mylist.size() - ++index << " elements yet to be guessed.\n";
    }
}

bool Question::tryAgain()
{
    return (++tries < 3);
}

void waitForEnter();

int main()
{
    Question myquestion;
    myquestion.local();
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Omg that makes alot more sense now, the prototyping one with chances=AFC()
I see why it was done to prevent clashing, so i assume that's another reason why STD:: is important.

Didn't know break was permitted outside of a switch hmm

I have a bad habit using conditions on operators for testing logical conditions, never really bothered with bool due to its simplicity and scope.

Not sure why alot of tutorials implicitly imply to use no STD::, even game engines don't use STD.

But i will use this as a example kinda blue print for code structure, as i lack code structure and casting to copying techniques.

Thanks masters: need to learn casting expressions now.
Last edited on
Thanks masters: need to learn casting expressions now.

What exactly do you mean by the above statement?

The biggest thing you need to learn about casting is how to avoid casting in most instances. You should be using the proper types for your variables to avoid the need to cast in most cases. Casting can be cause difficult to find problems because in many cases the compiler assumes that the programmer knows what they're doing and doesn't always warn of potential problems with the cast.


Topic archived. No new replies allowed.