Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Debug Project 3 (Easy to Medium)

Think you can spy out that little nasty bug? Try debugging some code in here

Debug Project 3 (Easy to Medium)

Postby Marss++ on Mon Jul 21, 2008 10:57 pm

Here is a code that is to get a single letter from the user and print it out to the screen with two functions.


Here it is:


Code: Select all

#include "stdafx.h"
#include <iostream>
using namespace std;


void prinTLetteRGiven()
{
   cout << "The letter you typed in was " << letter << endl;
}


int main()
{
   char letter;

   cout << "Please input 1 letter: ";
   cin >> letter;
   prinTLetteRGiven();

   char end;
   cin >> end;
   return 0;
}




When you figure it out. Write the new code and Say why this doesn't work.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby asib on Wed Jul 23, 2008 12:37 am

You need to add parameters and arguments like so :

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;


void prinTLetteRGiven(char letter)
{
   cout << "The letter you typed in was " << letter << endl;
}


int main()
{
   char letter;

   cout << "Please input 1 letter: ";
   cin >> letter;
   prinTLetteRGiven(letter);

   char end;
   cin >> end;
   return 0;
}


The first wouldnt work, because you would be outputting a variable that wasn't declared in that scope.
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby oopalonga on Wed Jul 23, 2008 11:33 am

That one didn't work b/c "letter" was undeclared, so the program had no way of identifying "letter" before it was inputed.

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;


void prinTLetteRGiven()
{
   char letter;
   cin >> letter;
   cout << "The letter you typed in was " << letter << endl;
}


int main()
{
 
   cout << "Please input 1 letter: ";
   prinTLetteRGiven();

   char end;
   cin >> end;
   return 0;
}
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby oopalonga on Wed Jul 23, 2008 11:35 am

So it appears from asib's post that you can declare a variable inside the function's parenthesis?

Like so:

Code: Select all
void prinTLetteRGiven(char letter)
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby asib on Wed Jul 23, 2008 12:40 pm

Well, you can declare parameters which are basically requirements of a function, and allow you to pass values between two scopes. Via my way, you declare your char type in main, then you hand over a "copy" of your char (the copy is called an argument), as a variable named whatever you gave your parameter name and then you use that in your function. So, whilst yours would work, you wouldn't have access the the variable later on. Alternatively, you could return that value from the function and then assign a variable to the function like so :

Code: Select all
function blah (  )
{
      char y;
      cin >> y;
     cout << y << endl;
     return y;
}

int main (  )
{
     char myChar = blah (  );

     return 0;
}
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby Marss++ on Wed Jul 23, 2008 1:32 pm

You are Right.


Great Job!!!
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby oopalonga on Thu Jul 24, 2008 12:04 pm

Interesting.
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby Marss++ on Thu Jul 24, 2008 12:40 pm

You seem confused oopalonga.

Do you have any questions?
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Doctor Salt on Sun Aug 17, 2008 11:30 pm

Due to scope issues, when i code something, i tend to do nothing except a menu in the main function. I then just call other functions, and do EVERYTHING there. That way, i don't have to worry about scope, or about passing variables around :P
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby rooski on Mon Aug 18, 2008 6:33 pm

Doctor Salt wrote:Due to scope issues, when i code something, i tend to do nothing except a menu in the main function. I then just call other functions, and do EVERYTHING there. That way, i don't have to worry about scope, or about passing variables around :P


but in bigger programs that would get confusing , and what if you accidently use that variable in another part of your progrma/game when it might stand for something like health or lvl , or magic , then you would have a serious bug and the compiler wouldnt catch it becuase you technically did nothing wrong , so try to declare variables in functions when you need them .

correct me if im wrong Mars++ or antiRTFM
rooski
 
Posts: 8
Joined: Thu Jul 24, 2008 11:08 pm

Postby Doctor Salt on Mon Aug 18, 2008 7:54 pm

Well, i would only use those variables when i need them, making each function somewhat standalone to a degree. Of course this wouldn't work for every situation, but it might make a few a lil easier. My way, you could take a function, copy paste it into a new project, rename the fucntion name to main, then test it just like that for problems. Thats how i tested my 700 line calculator, which had a TON of problems in it first time i tested it. Though its totally up to you on how you like to code it :P
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby Marss++ on Tue Aug 19, 2008 4:02 pm

Both rooski and Doctor salt are both right here to a certain degree.

You want to decalare and intialize variables as close to where there needed as possible. This way it's harder for them to get mixed up in the mess.

Due to scope issues, when i code something, i tend to do nothing except a menu in the main function. I then just call other functions, and do EVERYTHING there. That way, i don't have to worry about scope, or about passing variables around


I kind of understand what your saying here. It is common for games to be passing around to different functions and avoiding main, but this in no way gets rid of the limitations of scope. If a variable is declared within a function then that variable needs to be passed through arguments in order to get into a different function. I'm not going to get into passing by value and passing by reference but either way the variable has to be within those parenthesis or they will not exist in the function that is called.

I hope that cleared it up. If not... Post away!
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Doctor Salt on Tue Aug 19, 2008 9:09 pm

one last thing to say on this thread: Don't forget about classes! You could technically make global variables, immune to scope by putting those variables inside classes :P
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby antiRTFM on Tue Aug 19, 2008 11:26 pm

Doctor Salt wrote:one last thing to say on this thread: Don't forget about classes! You could technically make global variables, immune to scope by putting those variables inside classes :P


Well, what do you mean? Objects (instances) of that class are also subject to the rules of scope as much as any other variable. (at least when creating them locally within a function. If you make the instance out in global space, then its just like any other global variable)
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Postby Doctor Salt on Tue Aug 19, 2008 11:53 pm

Ah.... maybe we're agreeing, and i'm just over complicating things... again...


So, to take your work as an example, you make a class, like your soldier class, or ogre class, and just make all the variables public (or not, and just do the same thing, with functions) so you should be able to use them whenever you want, and it should be the same every time you call it, too.
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Next

Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests