Follow us on Twitter!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 20
Guests Online: 20
Members Online: 0

Registered Members: 82876
Newest Member: bhl1986
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Page 1 of 2 1 2 >
Author

C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 17:45
I am trying to make an IF statment to check the value i enter against the pre set value.

This is my code
Code

#include <iostream>

using namespace std;
char name[200],title[200];
int main()
{
 cout<< "Enter your name: ";
 cin.getline(name,200);
 if(name != "CD")
 {
     main();   
 }   
 cout<<"Enter the title: ";
 cin.getline(title,200);   

 cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";   
 cin.get();   
 return 0;   
}






But no matter what i enter as my name , it always goes back to the main function.

I tried to put the value im checking against in a var, but that caused an error.

Code

char check[200];
check = "CD";





and i also tried putting in , cin.ignore(); after getting the input, but that didnt work.
It still done the same as the first code.
Code

#include <iostream>

using namespace std;
char name[200],title[200];
int main()
{
 cout<< "Enter your name: ";
 cin.getline(name,200);
 cin.ignore();
 if(name != "CD")
 {
     main();   
 }   
 cout<<"Enter the title: ";
 cin.getline(title,200);   

 cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";   
 cin.get();   
 return 0;   
}






Any help would be good.
Thanks,
CD




Edited by on 04-09-08 17:58
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 17:55
When you post code, DISABLE SMILEYS. The only times you would not use a semi-colon to terminate a line in C++ would be these:

1. In the actual testing portion of a conditional statement or loop
2. When you're assigning a multi-line string or overlap your line of code to the next line

There's a mistake, to start with... whether it would cause the issue you're seeing or not is yet to be seen. Also, if you're going to be running a function over and over again, it's a better idea to write another function and use that... don't loop main(), cuz it looks goofy and could cause more issues than you really need.


Author

RE: C++, If statement problems

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 04-09-08 18:12
Why are you using character arrays instead of strings?


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 18:13
I think i need to read more of the tutorial for C++.


Ok, i have made this now
Code

#include <iostream>

using namespace std;
char name[200],title[200];
char error();
int main()
{
 cout<< "Enter your name: ";
 cin.getline(name,200);
 if(strcmp (name, "CD") != 0)
 {
   cout<< "Error\n";
 }
 else
 {   
 cout<<"Enter the title: ";
 cin.getline(title,200);   

 cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";
 }
 cin.get();   
 return 0;
}










Edited by on 04-09-08 18:17
Author

RE: C++, If statement problems

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 04-09-08 18:18
Just #include <string> and then you can declare variables of 'string' type. Then you can replace your current getline lines with something that looks like this:
getline(cin, variable name);

EDIT:
What's the 'char error();' for? You never call it up.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Edited by ynori7 on 04-09-08 18:20
ynori7 http://halls-of-valhalla.org
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 18:22
Oh yer, i deleted it now, was going to make it an error function , but decided not to.


Author

RE: C++, If statement problems

yours31f
Member



Posts: 1678
Location: Dallas Texas
Joined: 27.04.07
Rank:
Elite
Posted on 04-09-08 21:38
just my 2 sense,
1)use string instead of char[];
2) you don't have to declare it.
3) they were right, make a separate function and call it multiple times in main.

any more questions, feel free to pm me, I'll be on for a few more hours messing with php and mysql.



Debugging is what programmers do to beta software to make it take up more room on your hard drive if it is running too efficiently.


img259.imageshack.us/img259/3713/sigr.png

yours31f@live.com yours31f@yahoo.com rpwd.info
Author

RE: C++, If statement problems

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
Posted on 04-09-08 21:56
yours31f wrote:
2) you don't have to declare it.

Don't have to declare what?


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

RE: C++, If statement problems

yours31f
Member



Posts: 1678
Location: Dallas Texas
Joined: 27.04.07
Rank:
Elite
Posted on 04-09-08 22:07
#include <string> is not needed.


Debugging is what programmers do to beta software to make it take up more room on your hard drive if it is running too efficiently.


img259.imageshack.us/img259/3713/sigr.png

yours31f@live.com yours31f@yahoo.com rpwd.info
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 22:17
ynori7 wrote:
yours31f wrote:
2) you don't have to declare it.

Don't have to declare what?

yours31f wrote:
#include <string> is not needed.


Declaring is what you do when you're making variables... and, IIRC, <string> is needed to make string vars in C++.


Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 22:20
You can scope from std, just make sure you include <iostream>.


Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 22:28
sharpskater80 wrote:
You can scope from std, just make sure you include <iostream>.


Wow, that's crazy... because I know I'm rusty on my C++, but every site you go to on basic variable declarations insists that you have <string> included. However, it is probably just a case of where they are ALL wrong and you're right.

Anyways... does it really matter what he includes? It's the meat of his code that is being critiqued.




Edited by on 04-09-08 22:31
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 22:53
No, it doesn't really matter. Just wanted to let him know that string/cstring are C library headers. I understand the problem is him using one style of function for the other style of strings.




Edited by on 04-09-08 22:54
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 23:20
With regards to not including the string header file, you have to include it! There is an exception to this however. If you are using the microsoft IDEs to compile your programs then when you leave out headers they will try to find and include the ones you are intending to use. This is very bad practice. Always include the ones you need and dont let microsoft do it for you.
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 04-09-08 23:36
sharpskater80 wrote:
Just wanted to let him know that string/cstring are C library headers.


Curious... I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point... been meaning to.




Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 05-09-08 01:02
Zephyr_Pure wrote:
sharpskater80 wrote:
Just wanted to let him know that string/cstring are C library headers.


Curious... I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point... been meaning to.



He's using std. He doesn't need it. It works fine without including it with g++.

@ Coder Disaster: A.) I see why you picked that nick.
B.) Use strings. Much simpler and it's one big feature in C++ that is better than in C.
C.) Just use cin >> instead of cin.getline
D.) You should really be able to figure this out.
E.) Because I'm feeling nice:
Code

#include <iostream>

using namespace std;
string name;
string title;
int Prog()
{
 cout << "Enter your name: ";
 cin >> name;
 if(name != "CD" || name=="")
 {
   cout << "Error\n";
   return 0;
 }
 else
 {
 cout << "Enter the title: ";
 cin >> title,200;

 cout << "Your name is: " << name << "\nAnd You are: " << title << "\n";
 }
 cin.get();
 return 1;
}
int main()
{
        while((Prog())==0)
        {
        }
        return 0;
}



Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 05-09-08 01:06
Zephyr_Pure wrote:
sharpskater80 wrote:
Just wanted to let him know that string/cstring are C library headers.


Curious... I was under the impression that <Cstring> and <string.h> were used for string functions, and that <string> was a class that inherited std. Guess I should get back into C++ at some point... been meaning to.



#include <cstring> and <string.h> are synonymous. Usually, you either prefix c to use the standard C headers (cstdlib,cstdio,ctime) or you affix it with .h (stdio.h, stdlib.h,time.h).
Look around on cplusplus.com (it doesn't implicitly say that, but you can tell when it talks about the C functions and my compiler doesn't complain either).


Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 05-09-08 01:15
This is now my code , advanced a bit , but still no strings ,:( need to learn them next i think.



Code

#include <iostream> //include the input output stream

using namespace std; // using the namespace std
char name[200],password[200]; //define the vars for username and password
int main() // start the main function
{
 cout<< "Enter your name: ";// print the message ,enter your name
 cin.getline(name,200);  // get what the user inputs
 if(strcmp (name, "username") != 0)// check the input against the predefined username, if wrong
 {
   cout<< "Error, try again\n";//print error message, then new line
   main();//restart the main function
 }
 else//if the username is correct
 {   
  cout<< "Enter your password: ";// print the message enter your password
  cin.getline(password,200);    //get the password from the input
  if(strcmp(password,"password") != 0)//check the password, and if wrong do
  {
   cout<< "Error, try again\n";     // print error message and new line                 
   main();//restart function
  }
  else//if password is correct
  {                       
  cout<<"Loading custom settings...\n";// print loading settings
  system("title Your handle");//change the title to what your handle
  system("color 0a");//change the text color to green with black background
  cout << "Welcome handle.\n";//print the message Welcome ...
  //system("cd Program Files\Mozilla Firefox\ ");
  cout<< "Opening pre-set web pages.\n";//open web pages
  system("start firefox.exe http://www.computerhope.com/overview.htm");
  system("start firefox.exe http://www.hellboundhackers.org");
  system("start firefox.exe http://www.cprogramming.com/tutorial/");
  cout<< "Web pages opened\n";//print message when oppened
  }
 }
 cin.get();   
 return 0;
}




Also i was going to add a function if you get it wrong that , i will do later, but it works so im happy :D


Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 05-09-08 01:17
[quote]Coder Disaster wrote:
I am trying to make an IF statment to check the value i enter against the pre set value.

This is my code
Code

#include <iostream>

using namespace std;
char name[200],title[200];
int main()
{
 cout<< "Enter your name: ";
 cin.getline(name,200);
 if(name != "CD")
 {
     main();   
 }   
 cout<<"Enter the title: ";
 cin.getline(title,200);   

 cout<<"Your name is: "<< name << "\nAnd You are: "<< title << "\n";   
 cin.get();   
 return 0;   
}






Code

#include <iostream>
#include <cstring>
using namespace std;

int main(int argc, char **argv){
string name,title;
cout<<"Enter your name: ";
getline(cin,name);
if(strcmp(name.c_str(),"CD")!=0){
     main();
}
cout<<"Enter the title: ";
getline(cin,title);
cout<<"Your name is: "<<name<<"\n And your are: "<<title<<"\n.";
cin.get();
return 0;

}




You might have to tweak it some, but it should work. You were confusing C syntax with C++ syntax. I am to lazy to code the C example, but lookip printf, scanf, strcmp, and the .c_str() method for the ability to use C functions on C++ strings.





Edited by on 05-09-08 01:18
Author

RE: C++, If statement problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 05-09-08 01:30
Still mixing them.. Just use operator == to compare.


Page 1 of 2 1 2 >