Syntax error help

Hi my c++ class gave me a problem to:

Create a while loop that will call a protect Data Function --> int protectData(string)

Function will catch a prompt from main and then allow the user to enter their score

The score should be a valid # and also within the range 0-100. Lock the user into a while Loop until a valid # is entered

Return the valid # to main and then prompt the user if they would like to repeat y/n

I made a lot of syntax errors, can someone help me correct them?

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
  #include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int score;
string yorn;
int protectData(string){
	if (score >= 0 || <= 100)
		cout << "It's valid, would you like to go again? y or n?";
		cin << yorn;
	if (yorn = 'y') {
		main();
	};
	else {
		break
	};

};
int main()
{
	cout << "Please enter a number between 0-100";
	cin >> score;
	cout << "would you like to protect this data? y or n?"
	cin >> yorn;
	while (yorn = 'y') {
		protectData(yorn);

		return 0



};

Hello Wargrav,


I made a lot of syntax errors, can someone help me correct them?



First thing you need to do is make your code easier to read. A few blank lines goes a long 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 <string>
//#include <stdio.h>  // <--- Not required and may be included through "<iostream>".

using namespace std;  // <--- Best not to use.

int score;  // <--- Try to avoid global variables.
string yorn;

int protectData(string)  // <--- is "string" a type or a variable name. As is it is an error.
{
    if (score >= 0 || <= 100)
        cout << "It's valid, would you like to go again? y or n?";
    cin << yorn;  // <--- Not part of the if statement therefor it needs a prompt.

    if (yorn = 'y')
    {
        main();
    };
    else
    {
        break
    };

};

int main()
{
    cout << "Please enter a number between 0-100";
    cin >> score;

    cout << "would you like to protect this data? y or n?"
        cin >> yorn;

    while (yorn = 'y')
    {
        protectData(yorn);

        return 0
    };
}  // <--- Missing the closing } for "main". 

Also in your use of the {}s be consistent. Do them the same for everything. As above.

Notice lines 13 and 14. Indenting the "cin" does not make it part of the if statement only enclosed in {}s is it part of the if statement.

The (;) following the closing } of if, else, for loops and while loop is not required I do not believe it causes any problem just ignored.

You need to compile your program and fix any error and warnings that you can. What you do not understand post the complete error message that the compiler gives and ask for an explanation.

You have some lines that do not end in a (;). They need fixed.

Fix what you can and post your changes in a reply. DO NOT edit or change your original post that just makes it confusing to anyone that comes in late.

"break" is used in a for loop, do/while and while loop and a switch. An "else" statement is not something that you can break out of. It just ends. Even if you add the (;) after "break" it is still an error.

Andy
@Wargrave

In lines 16 and 35, you are assigning yorn with ' 'y', NOT checking its value. A single equal sign assigns while a double equal sign, '==' checks the value.
Also do not call the main function, it is one of the ways of getting a stack overflow.
> it is one of the ways of getting a stack overflow.

It is a compile-time error. http://coliru.stacked-crooked.com/a/f634b865182f1d33

The function main shall not be used within a program.
https://eel.is/c++draft/basic.start.main#3
Hello Wargrav,

You appear to understand using code tags for your code. This link will hwlp you understand more about the tags: http://www.cplusplus.com/articles/z13hAqkS/

The more I look at your code and understand the problems I feel that you should start here: https://www.learncpp.com/ This is a good tutorial that is kept up to date with the latest standards. Not only is it a good tutorial it later becomes a good reference.

To be more specific look at: https://www.learncpp.com/cpp-tutorial/introduction-to-if-statements/ Chapter 7 has more about if statements and it would be good to read through all of chapter 7.

Chapter 6.1 will help with using {}s to create a block of code.

Chapter 6.8: https://www.learncpp.com/cpp-tutorial/why-non-const-global-variables-are-evil/ will help explain why global variables are bad.

Between your textbook or book and the links above ahould help to understand what you are doing wrong.

As to the code that you posted some tips that should help:
seeplus once wrote:

any required #define statements
std:: headers (eg iostream)
c headers (eg conio.h)
windows headers (eg windows.h)
3rd party headers
user headers



I look at that this way:

any required #define statements
std:: headers:
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
//#include <vector>  // <--- For future use.
C++ versions of C header files:
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.
#include <cmath>
#include <ctime>
#include <cstdlib>  // <--- usually used for "rand()" and "srand"
C headers:
#include <conio.h>  // <--- Not a good example as this should not be used and is not available to everyone.
windows headers:
#include <windows.h>
3rd party headers

Any header files need for the program:
#include <algorithm>
#include <chrono>
#include <random>

user headers:
Any header file that you write and would be included using double quotes:
#include "yourHeader.hpp"

I also realized that the alphabetical order of the first group helps to figure out what is missing when you forget a header file.

The line using namespace std; I would add to that:
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ For future reference.

For the function:
https://www.learncpp.com/cpp-tutorial/introduction-to-functions/

Generally a function should do just 1 thing. Actually your function could be written to return a bool variable that can be used in "main" with an if/else statement to say if it is valid or not.

The function name should reflect what the function does. Your name protectData would lead one to think that you are protecting something when you are actually verifying the input, so VerifyData or VerifyInput would be a more appropriate name.

In "main" you start with "\n Please enter a number between 0 - 100: ". This could be interpreted as the numbers between 0 and 100 meaning 1 thru 99 inclusive or some may take it as meaning everything from 0 to 100. You may want to add inclusive after 100.

Your next prompt: "\n Would you like to protect this data? y or n? ". Again misleading because you are not protecting anything just verifying that it is with the range that you want. Change "protect" to "verify" for a better reading prompt that makes more sense.

The prompt "\n Would you like to go again? y or n? " and input should be at the end of "main" to control the condition of a do/while or while loop until you enter "n" to end the program.

You have defined "yorn" as a string. This is a bad choice. Someone could enter "yes" or "Yes" or any other combination of upper and lower case letters and it would never be equal to just "y". Defining "yorn" as a "char" is a better choice.

I would like to see what you can do to fix your program first before I post what I have done.

Andy
Topic archived. No new replies allowed.