Jump to content

Someone tell me what i'm doing wrong?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Hi i'm currently developing a console mode php program that displays all positive odd numbers which are less than or equal to whatever the user inputs so for example if the user inputs 16 it should show up like this,

1,3,5,7,9,11,13,15

Here is the code i have so far,


#!/usr/local/bin/php -q

<?php

//phpset3_1.php

require ("phpio");

$count = 1;

print("\n ");

print ("enter a number : ");

$count = input();

while ($count <=$)

{

  print("\n ");

  print ($count);

  $count = $count +2;

}

print("\n\n");

?>


Anyone tell me what ive done wrong and how to fix it. Much appreciated if you can

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Can you tell use the error you are getting?

#3
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
The error i'm getting is the odd numbers keep adding like an infinite loop so if the user types in 16 it will go past that and keep adding by 2 till i stop it.

Thanks.

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
you have an error in
while ($count <=$)
you need a variable name after your $ sign. as well, you are using the same variable for counting as for max value...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
I tried doing it but now it just displays whatever number the user inputs. Would you be able to show me exactly which parts of the code i need to change and what i should change it too.

Thanks. :)

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
just do a $input = input(); and then while ($count < $input)...
do you understand why and how that would work?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
kentona

kentona

    Newbie

  • Members
  • PipPip
  • 19 posts
Ah yes i do understand it now whatever the $input number is will be the result for the $count.

Thanks for all the help Orjan :)