The Official Programming Thread

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
@puppet @gullfounder

I tried making a calculator but it will only work for two numbers.... What if i infinitely wanted to add random numbers... will i have to declare infinite variables holding their result...absolutely no i guess?
Do i need to learn OOP before that? I haven't touched classes yet...
in C++ we could make link list array which could be unlimited. but as i said in previous post strict it to only 50 numbers. you need 50 variables or Array of 50.
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
I've made a perfectly working calculator in GUI for my programming class. There is no need for 50 variables or linked lists or anything fancy. As I mentioned in previous post, you can accomplish this with 3 variables only. 2 are operands, the third is the answer.

When you're done with 2 numbers and have an answer, you do not need the old values anymore. Store the answer in first variable, and keep the third variable for new numbers.
 

staticPointer

PG LEGENDARY
Dec 7, 2012
3,266
0
41
افغانستان
www.pakgamers.com
I have learnt Ruby not completely... i have yet to learn OOP in ruby plus i thought Ruby on Rails was different? it was for web development i guess? Anid Ruby is my first or to be more exact 2nd language.... i haven't learnt python.... I intend to complete my RPGMaker project after learning Ruby as that engine uses Ruby
yes ror is used for the web and it(s) is framework including mvc !! so carry on with the project man !!
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
@staticPointer
thanx :D
I've made a perfectly working calculator in GUI for my programming class. There is no need for 50 variables or linked lists or anything fancy. As I mentioned in previous post, you can accomplish this with 3 variables only. 2 are operands, the third is the answer.

When you're done with 2 numbers and have an answer, you do not need the old values anymore. Store the answer in first variable, and keep the third variable for new numbers.
I get what you are trying to say will do that shortly...
 
Last edited:

puppet

Well-known member
Sep 30, 2013
2,169
0
42
^^ as megaman said


sum = sum + number;

no you dont need to write it in oop its just structure not logics , finish your calculator in simple code
 

puppet

Well-known member
Sep 30, 2013
2,169
0
42
Everyone knows how a simple calculator works. But try adding somthing extra into it.
he will add it later he is still not understanding about the variables , step by step learning is fine as he isnt joining proper classes for programming
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
@MegamanEXE @puppet

the variable problem is solved but there is one more problem before i can make a calculator that can add up numbers to a large range..... I don't know how to put that important bit of code inside a loop so that unless the user doesn't types "answer" it keeps on asking for a new number and keeps on performing the operation for e.g here is the current code... its only for addition btw


EDIT:- awrite no problem another method worked.... i'll complete the code now, thanx for the help
 
Last edited:

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
@puppet [MENTION=1394]MegamanEXE[/MENTION]
Awrite!... thanx to you guys i finally created a simple calculator that can +,-,* and / infinitely.... Note that since i assumed there will be no other keys except numbers and operator... it will give an error if you type a string or something silly...
Spoiler: show
Code:
def operation                                                                 # This is a method i created which i will use several times in main body of the codeputs "Type either answer or the next operation"
choice_2 = gets.chomp.downcase


loop do 
     if 
    choice_2 == "+" 
    $n2 = gets.chomp.to_i
    $ans = $ans + $n2
    puts "Type either answer or the next operation"
    choice_2 = gets.chomp.downcase
    break if choice_2 == "answer"
    redo if choice_2 == "+"
    next if !choice_2 == "+"
elsif 
  choice_2 == "-"
    $n2 = gets.chomp.to_i
    $ans = $ans - $n2
    puts "Type either answer or the next operation"
    choice_2 = gets.chomp.downcase
    break if choice_2 == "answer"
    redo if choice_2 == "-"
    next if !choice_2 == "-"
 elsif 
  choice_2 == "*"
    $n2 = gets.chomp.to_i
    $ans = $ans * $n2
    puts "Type either answer or the next operation"
    choice_2 = gets.chomp.downcase   
    break if choice_2 == "answer"
    redo if choice_2 == "*"
    next if !choice_2 == "*"
 elsif 
  choice_2 == "/"
    $n2 = gets.chomp.to_i
    $ans = $ans/$n2
    puts "Type either answer or the next operation"
    choice_2 = gets.chomp.downcase  
    break if choice_2 == "answer"
    redo if choice_2 == "/"
    next if !choice_2 == "/"


end
end
puts "#{$ans}"
end                                                                        # The method ends here #


puts "Enter a number"
n1 = gets.chomp.to_i


puts "what do you want to do"
puts "Type + to add"
puts "Type - to subtract"
puts "Type * to multiply"
puts "Type / to divide"


choice = gets.chomp.downcase
case choice

when "+" 
puts "Enter another number"
$n2 = gets.chomp.to_i
$ans = n1 + $n2
operation

when "-"
 puts "Enter another number"
$n2 = gets.chomp.to_i
$ans = n1 - $n2
  operation

when "*"
  puts "Enter another number"
$n2 = gets.chomp.to_i
$ans = n1 * $n2
  operation

when "/"
  puts "Enter another number"
$n2 = gets.chomp.to_i
$ans = n1 / $n2
  operation
end
Awrite guys any more ideas? What should i do now? I can't think of anyway to make it more compact...
 
Last edited:

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
you mean the user enters a paragraph and the program tells it how many "the" "and" etc are there? Codeacademy had a similar exercise... although i'll try making it from scratch again thanx for the idea btw
Yeah. But program must also count words. Like w a h k. So in the end. It shows.

20 words
50 letters.
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
Pre-stored words. Choose one randomly and guess away! It's a highly recommended example for beginners, and it's simple too!
I haven't played hangman before i just looked up on net :D so i was thinking if there are pre stored words like 10 or so i guess.... then the i am thinking of making it like this that if the user enters a letter the program will look through the word to see if it includes that... after that how will it say that this letter is placed here? What i am thinking is that it'll print the index number saying that its at fifth position, 4th etc... is that right?
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
I haven't played hangman before i just looked up on net :D so i was thinking if there are pre stored words like 10 or so i guess.... then the i am thinking of making it like this that if the user enters a letter the program will look through the word to see if it includes that... after that how will it say that this letter is placed here? What i am thinking is that it'll print the index number saying that its at fifth position, 4th etc... is that right?
Eh, you've never played Hangman (or Hangaroo)? o_O

Anyhow, that's actually how most programming is done. Test it on small sample, if it works for 10 words, it'll most probably work for 10,000 words as well. When you're beginning the program, you could have ONE word for testing purposes, just to see if the placement is correct, or are the number of guesses working correctly or not?

When that's done, it's simple for more words. This was an assignment for us as well (although we were given a difficult cheating version of Hangman to make). What I did was somewhat this.
Make 2 strings, one for the answer, and the other is your game string consisting of hyphens (-). For example,
String1 = Blizzard
String2 = --------

For the number of dashes, it's simple, count the number of letters in the answer(string1), and 'concatenate'/add the appropriate number of dashes. And yes, when you take a guess, it searches if it's in the solution, if it is, it will simply place at THE same index as the solution i.e. if you put 'A':
guessed = solution;

This kind of thing. The 'i' is where it found the 'a' (if any).

I'll put my code here (it's not exactly cheating since you don't know C++ :looking:). You can use the code to get some kind of idea IF you're stuck.

Spoiler: show
Spoiler: show
Code:
void Hangman::prompt()
{
	string buffer;


	cout << "Enter word length: ";
	cin >> wordLength;
	cout << "Enter number of guesses: ";
	cin >> guesses;
	cout << endl;


	ifstream dictionary("dictionary.txt");
	if(!dictionary) {cout<<"Error opening Dictionary.\n"; return;}


	//Read all required words into vector
	while(getline(dictionary,buffer))
	{ 
		if(buffer.length()==wordLength)
			wordList.push_back(buffer);	
	}


	if(wordList.size()==0){
		cout << "No words of size " <<wordLength <<" present";
		return;
	}


	//Choose a solution word
	srand(time(NULL));
	int randomIndex = rand() % wordList.size();
	solutionWord = wordList[randomIndex];


	//Initialize playing word to hyphen
	for(size_t i=0; i<solutionWord.length(); i++)
	{
		if(i==solutionWord.length())
			playWord[solutionWord.length()]='*';
		playWord = playWord + "-";
	}
}

void Hangman::run()
{
	prompt();


	char inputChar;
	int charIndex;


	cout << "(For Checking)SOLUTION: " <<solutionWord <<endl;	//TESTING PURPOSES


	while((playWord!=solutionWord) || guesses==0)
	{
		cout << "\nLetters used: " <<usedCharacters;


		cout<< "\nRemaining tries: " <<guesses <<endl;
		cout<< playWord;


		cout<< "\n\nEnter a guess: ";
		cin.ignore(100,'\n'); //For buffer problems
		
		inputChar = getchar();		
		
		charIndex = solutionWord.find_first_of(inputChar);
		
		if(charIndex == -1){
			cout<< inputChar <<" is not present" <<endl; 
			if(usedCharacters.find(inputChar) == -1){ //Only decrement if letter wasn't used before and add it
				guesses--;
				usedCharacters+=inputChar;
			}
			if(guesses==0)
				break;
		}
		else{
			for(size_t i=0; i<solutionWord.length(); i++)	{
				if(inputChar==solutionWord[i])
					playWord[i]=inputChar;
			}
		}	
	}


	if(guesses==0)
		cout << "Game Over";
	else
		cout << "\n************************************************************"
			<<"\nCongratulations! You found the solution i.e. "<<solutionWord
			<< "\n************************************************************" <<endl;
}
This is just a PART of the code, it won't compile, of course :p
 
Last edited:

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
All right i have my hangman program almost complete... there is just one problem...
I know you guys don't know ruby but if you can help make me understand how the hell does the code below works it'd be great... @MegamanEXE [MENTION=5658]gullfounder[/MENTION]
Code:
string = "abcde"string_modified = string
string_modified[2] = "2"            # variable[index number] = "thing you want at that index number" 
puts string_modified                  # prints "ab2de"
puts string                                # prints "ab2de"
I don't get why "string" variable gets modified even though i only changed the modified version.... however if i do something like this

Code:
r = "abcde"
r_mod = r
r_mod += "fghi"
puts rmod               # prints "abcdefghi"
puts r                     # prints "abcde"
I want the former thing to happen with the first one.... i need 2 different variables having same values but if i modify one the original one shouldn't get modified.. How do you make this happen...
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
All right i have my hangman program almost complete... there is just one problem...
I know you guys don't know ruby but if you can help make me understand how the hell does the code below works it'd be great... @MegamanEXE @gullfounder
Code:
string = "abcde"string_modified = string
string_modified[2] = "2"            # variable[index number] = "thing you want at that index number" 
puts string_modified                  # prints "ab2de"
puts string                                # prints "ab2de"
I don't get why "string" variable gets modified even though i only changed the modified version.... however if i do something like this

Code:
r = "abcde"
r_mod = r
r_mod += "fghi"
puts rmod               # prints "abcdefghi"
puts r                     # prints "abcde"
I want the former thing to happen with the first one.... i need 2 different variables having same values but if i modify one the original one shouldn't get modified.. How do you make this happen...
This is kind of like pointers/references you see in C/C++/Java. But that's the thing about them, you have to explicitly them to do this, they can't do this on their own, they weren't designed to be smart.
Now, this is probably because Ruby was designed to automatically point to the same string (even though it doesn't seem obvious). Ruby does its own 'optimization' thing. For more clarity, read this:

Seeing double: how Ruby shares string values - Pat Shaughnessy
 

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
Eh, you've never played Hangman (or Hangaroo)? o_O

Anyhow, that's actually how most programming is done. Test it on small sample, if it works for 10 words, it'll most probably work for 10,000 words as well. When you're beginning the program, you could have ONE word for testing purposes, just to see if the placement is correct, or are the number of guesses working correctly or not?

When that's done, it's simple for more words. This was an assignment for us as well (although we were given a difficult cheating version of Hangman to make). What I did was somewhat this.
Make 2 strings, one for the answer, and the other is your game string consisting of hyphens (-). For example,
String1 = Blizzard
String2 = --------

For the number of dashes, it's simple, count the number of letters in the answer(string1), and 'concatenate'/add the appropriate number of dashes. And yes, when you take a guess, it searches if it's in the solution, if it is, it will simply place at THE same index as the solution i.e. if you put 'A':
guessed = solution;

This kind of thing. The 'i' is where it found the 'a' (if any).

I'll put my code here (it's not exactly cheating since you don't know C++ :looking:). You can use the code to get some kind of idea IF you're stuck.

Spoiler: show
Spoiler: show
Code:
void Hangman::prompt()
{
string buffer;


cout << "Enter word length: ";
cin >> wordLength;
cout << "Enter number of guesses: ";
cin >> guesses;
cout << endl;


ifstream dictionary("dictionary.txt");
if(!dictionary) {cout<<"Error opening Dictionary.\n"; return;}


//Read all required words into vector
while(getline(dictionary,buffer))
{ 
if(buffer.length()==wordLength)
wordList.push_back(buffer);
}


if(wordList.size()==0){
cout << "No words of size " <<wordLength <<" present";
return;
}


//Choose a solution word
srand(time(NULL));
int randomIndex = rand() % wordList.size();
solutionWord = wordList[randomIndex];


//Initialize playing word to hyphen
for(size_t i=0; i<solutionWord.length(); i++)
{
if(i==solutionWord.length())
playWord[solutionWord.length()]='*';
playWord = playWord + "-";
}
}

void Hangman::run()
{
prompt();


char inputChar;
int charIndex;


cout << "(For Checking)SOLUTION: " <<solutionWord <<endl;//TESTING PURPOSES


while((playWord!=solutionWord) || guesses==0)
{
cout << "\nLetters used: " <<usedCharacters;


cout<< "\nRemaining tries: " <<guesses <<endl;
cout<< playWord;


cout<< "\n\nEnter a guess: ";
cin.ignore(100,'\n'); //For buffer problems

inputChar = getchar();

charIndex = solutionWord.find_first_of(inputChar);

if(charIndex == -1){
cout<< inputChar <<" is not present" <<endl; 
if(usedCharacters.find(inputChar) == -1){ //Only decrement if letter wasn't used before and add it
guesses--;
usedCharacters+=inputChar;
}
if(guesses==0)
break;
}
else{
for(size_t i=0; i<solutionWord.length(); i++){
if(inputChar==solutionWord[i])
playWord[i]=inputChar;
}
}
}


if(guesses==0)
cout << "Game Over";
else
cout << "\n************************************************************"
<<"\nCongratulations! You found the solution i.e. "<<solutionWord
<< "\n************************************************************" <<endl;
}
This is just a PART of the code, it won't compile, of course :p

I got a simple challange for you.

Get 2 variables with random numbers. Try to exchange the values with out using any other variablea.
 
General chit-chat
Help Users
We have disabled traderscore and are working on a fix. There was a bug with the plugin | Click for Discord
  • No one is chatting at the moment.
    NaNoW NaNoW: ....