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.
  • Necrokiller Necrokiller:
    The only valid thing from his pov he said in the video is AC dead since Black Flag. According to woke police that game was woke too. Welsh man in West Indies. So atleast he's consistent I guess lol
    Link
  • Link
  • XPremiuM XPremiuM:
    Necrokiller said:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    Nope. They have a very valid case. The above video explains it all.
    Link
  • Necrokiller Necrokiller:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    • Like
    Reactions: SolitarySoldier
    Link
  • XPremiuM XPremiuM:
    Meanwhile Ghost of Tsushima PC version is out now. Looks 100 times better than ASS Creed already.
    Link
  • XPremiuM XPremiuM:
    Did y'all see the new Assassin's Creed trailer? They finally made a AC set in Japan & then they put a negro as the male protagonist. Ubisoft is taking cues from Disney, and it isn't gonna end well for them, just like Disney. Go woke, go broke!
    Link
  • Necrokiller Necrokiller:
    First Fallout 4 update and now this 🤡
    Link
  • Necrokiller Necrokiller:
    MS and Bethesda continuing their streak of massive Ls 😬
    Link
  • Link
  • funky funky:
    Hello
    Link
  • NaNoW NaNoW:
    by closing down good studios
    Link
  • NaNoW NaNoW:
    well he is breaking barriers
    • Like
    Reactions: KetchupBiryani
    Link
  • iampasha iampasha:
    SolitarySoldier said:
    Phil keeps talking about breaking barriers to gaming, making it accessible on all platforms yada yada, while killing competition and creativity at the same time. the fact that i actually believed him for a second lol
    guys the biggest yapper in the Industry right now. All he do is yap
    Link
  • Necrokiller Necrokiller:
    Phil should be held responsible for this shitfest too, just like Sarah, but it's highly likely that these decisions are coming from Satya. And this isn't even the end of it. More closures are coming.
    Link
  • SolitarySoldier SolitarySoldier:
    if we are moving towards more and more popular trash across platforms that make billions for companies, I'm happy with all the barriers and exclusivity because at least that brings some pressure to create good stuff.
    Link
  • SolitarySoldier SolitarySoldier:
    Phil keeps talking about breaking barriers to gaming, making it accessible on all platforms yada yada, while killing competition and creativity at the same time. the fact that i actually believed him for a second lol
    Link
  • SolitarySoldier SolitarySoldier:
    "These changes are not a reflection of the creativity and skill of the talented individuals at these teams or the risks they took to try new things" ... seems to me that's exactly what it is
    Link
  • SolitarySoldier SolitarySoldier:
    why make good games when u can just buy everyone and shut them down lol
    Link
  • XPremiuM XPremiuM:
    I'm gonna say one last time, F*** Microsoft to infinity!
    Link
  • XPremiuM XPremiuM:
    Microsoft deserves all the hate they can get. Seriously i can't explain how much i want to curse them out.
    Link
  • XPremiuM XPremiuM:
    They could've sold the studios instead of closing them, but the nazi bastards just didn't want competition down the road.
    Link
  • Link
  • XPremiuM XPremiuM:
    F*** Microsoft, and F*** their fanboys.
    Link
  • XPremiuM XPremiuM:
    What's the f*** is wrong with them? I mean really? Have they completely lost it? F***ing retards.
    Link
  • XPremiuM XPremiuM:
    So i just found out that f***ing s***bag Microsoft shut down Arkane Studio (makers of the brilliant Dishonored series) and Tango Gameworks (makers of the iconic The Evil Within series), among some other studios. I just want to say a giant F*** Y**! to Microsoft. THEY'VE F***ED UP BIG TIME this time.
    Link
    Necrokiller Necrokiller: The only valid thing from his pov he said in the video is AC dead since Black Flag. According to...