Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-04-2021, 10:10 PM   #1
LQ Newbie
Registered: Nov 2021
Posts: 1

Rep: Reputation: Disabled
Guessing Game

This is my code is it say that
./guessing_game: line 17: syntax error: unexpected end of file

for (( ; ; ))
read -p "Guess a number between 0 and 99, inclusive:"
if [$user_number -eq $system_number ]
echo "your number is right!"
echo "your number is wrong"
if [ $user_number -ge $system_number ]
echo "Your guess to to high."
echo "Your guess to to low."
Old 11-04-2021, 10:48 PM   #2
Registered: Jul 2018
Distribution: Slackware
Posts: 264

Rep: Reputation: 209Reputation: 209Reputation: 209
You didn't provide 'read' with a variable to store user input such as 'user_input'. Also you needed to close the first 'if' statement with a closing 'fi' and the 'for' loop with a closing 'done'.

I changed a couple of other things such as breaking if the user guesses the correct number, since 'system_number' never changes and '-ge' to '-gt' in the second 'if' test because once it's determined the user entered the wrong number there's no point in testing for equality.

Also you should add a restriction on user input if they can only enter numbers 0-99.

PHP Code:


for (( ; ; ))
read -"Guess a number between 0 and 99, inclusive:" user_number

if [ "$user_number-eq "$system_number]
echo "your number is right!"
"your number is wrong"

if [ "$user_number-gt "$system_number]
echo "Your guess to to high."
"Your guess to to low."

Last edited by Mechanikx; 11-05-2021 at 02:25 PM. Reason: Brain fart.
Old 11-05-2021, 08:11 AM   #3
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,782

Rep: Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456Reputation: 1456

Surround code with [code]..[/code] blocks to post code, so formatting is not lost.

There are three syntax errors in the code presented - lack of whitespace after bracket in the first "if", lack of "fi" for the same, and lack of "done" for the loop. (The latter two would suggest you neglected to copy the whole thing, except the error says otherwise; did you write this code from scratch or copy it from somewhere?)

As per the Bash manual read without a variable name specified will default to setting REPLY. If you want to change that you must specify the name.

ShellCheck is a tool that can be used both online or offline to highlight all these (and a couple of other issues).



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Write a guessing game in ncurses on Linux LXer Syndicated Linux News 0 08-31-2021 01:25 AM
[SOLVED] Random Color Guessing Game Seattlesweet Linux - Newbie 8 06-07-2016 06:38 PM
I'm guessing my video card is going bad? Linux~Powered Linux - Hardware 3 04-28-2004 06:32 PM
strcat and direntp->d_name segmentation fault [I'm guessing the problem, plz propose] zeppelin Programming 3 04-12-2004 03:52 PM
7082 unexpected failers, i'm guessing this is bad veritas Linux From Scratch 5 03-18-2004 06:28 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration