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

Postby asib on Thu Aug 21, 2008 4:18 pm

Doctor salt, I think you're getting a bit confused. You are partly right, in that everywhere you go, you would be able to access those variables inside the class but to be able to access them, you need an object (unless they are static) and the object is subject to the rules of scope.

Despite this, you shouldn't be worrying about scope, because it's a good thing. Without scope, we might accidentally change something and not realise it, because the compiler sees it as a valid statement and it will run at runtime.

Although scope has its limitations, you can sort of bypass these with references and pointers. I'll say no more.
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby Marss++ on Fri Aug 22, 2008 9:07 am

Yeah... I think he gets it, it's just confusing to talk about.
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

Re: Debug Project 3 (Easy to Medium)

Postby OmgItsVash on Sun Sep 14, 2008 11:01 am

I replaced the above lines, to beneath where the program decleared the char: "letter"

Correct me if i'm wrong but it works fine for me:

Code: Select all
#include <iostream>
using namespace std;


void prinTLetteRGiven();

int main()
{
   char letter;

   cout << "Please input 1 letter: ";
   cin >> letter;
     
   cout << "The letter you typed in was " << letter << endl;


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


the reason it only has one include is that i'm using another compiler named: dev-C++
OmgItsVash
 
Posts: 2
Joined: Sun Sep 14, 2008 10:30 am

Argio's Turn

Postby Argio on Mon Dec 15, 2008 5:52 am

Oh hai guys!

Here is the code:

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;
    }


(Ok so for this one there are many possible ways of coding this. I believe that this is the best... )

In case you didn't see the line is on error 7 and 8, oh wait I meant the error is on line 7 and 8. Lol.
Code: Select all
    void prinTLetteRGiven()
    {
       cout << "The letter you typed in was " << letter << endl;
    }


Oh noes! What do we do Argio?????????????? Halp!

Well, this is what I think is the best way to do this. This also may optomize the program to its fullist extent. Isn't that what we wanted?


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;
    }


What the hell Argio how does this work???

Well, its is very simple.
Lets think of this as real life objects...
A MacOSX computer = letter

So I want to print out the character that is MacOSX right?
Ok but here is the problem the compiler hasn't told the program what letter is. So essentially the computer has no idea what type it is, what it could contain, or if its anything really. So now because we havn't told the program what lMacOSX is or anything about it, it could be a cake, it could be a pie, it could be the worst operating system in the universe, it could contain the value of 7, or any value for that matter. So if I want to print this out what is going to happen????

Well, this starts something called Undefined Behavior. And this could result in major glitches within your program or even in some cases operating system crashes.

This is what I did to fix it:


I made it so that letter is defined as a incoming chat from the function calling the prinTLettRGiven function. So when I called it in the main function I had to tell it to bring along a variable in this case the main function's letter variable. Thus when the prinTLettRGiven function gets called it brings along letter and then tells it to output letter :D!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


YAY
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: Debug Project 3 (Easy to Medium)

Postby Symbol on Sun Jun 28, 2009 7:58 am

This was yet another good and easy Project.
The error was due to the code below.
Code: Select all
char letter;


There was noting wrong with how it was written, but the problem was that code was out of visible range. It was not in the same scope so the "void prinTLetteRGiven()" couldn't see it, basically you had to move it to a more see able spot, a place where all functions can easily see it. I don't know if I said it correctly :lol:




Here is how the code should be.

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

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


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

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





Basically I moved it right below the "using namespace std;" making it visible to all the functions.
Btw, I am not cheating :lol: I maybe new, but I am doing it all on my own. Even though its just basic stuff.
Symbol
 
Posts: 8
Joined: Sun Jun 28, 2009 2:30 am

Re: Debug Project 3 (Easy to Medium)

Postby antiRTFM on Sun Jun 28, 2009 8:15 am

@Symbol: Actually though your solution works, it is not the way to go...

What you've done is created a "Global Variable" which means that "char letter" belongs to public/general scope, not limited to any other scope. This means everyone everywhere can access and modify it, which is a bad thing to allow in your program. Variables should be limited to the scope where they'r needed.

Can you come up with a better solution which doesn't make a global variable?
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Re: Debug Project 3 (Easy to Medium)

Postby Symbol on Mon Jun 29, 2009 2:54 am

Okay I see what your saying.

What if I replace

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



with

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



It seems to also work.
No more global scopes for me I guess.
Symbol
 
Posts: 8
Joined: Sun Jun 28, 2009 2:30 am

Re: Debug Project 3 (Easy to Medium)

Postby antiRTFM on Mon Jun 29, 2009 6:41 am

Excellent! There ya go
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Previous

Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests