LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-04-2021, 09:10 PM   #1
camsky209
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

system_number=87
no_of_guess=1
for (( ; ; ))
do
read -p "Guess a number between 0 and 99, inclusive:"
if [$user_number -eq $system_number ]
then
echo "your number is right!"
else
echo "your number is wrong"
if [ $user_number -ge $system_number ]
then
echo "Your guess to to high."
else
echo "Your guess to to low."
fi
 
Old 11-04-2021, 09:48 PM   #2
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 357

Rep: Reputation: 260Reputation: 260Reputation: 260
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:

#!/bin/bash

system_number=87
no_of_guess
=1
for (( ; ; ))
do
read -"Guess a number between 0 and 99, inclusive:" user_number

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

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

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

Rep: Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578Reputation: 2578

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).

 
  


Reply



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 12:25 AM
[SOLVED] Random Color Guessing Game Seattlesweet Linux - Newbie 8 06-07-2016 05:38 PM
I'm guessing my video card is going bad? Linux~Powered Linux - Hardware 3 04-28-2004 05:32 PM
strcat and direntp->d_name segmentation fault [I'm guessing the problem, plz propose] zeppelin Programming 3 04-12-2004 02:52 PM
7082 unexpected failers, i'm guessing this is bad veritas Linux From Scratch 5 03-18-2004 05:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

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