Jump to content

C++ Assistance needed


Dariuas

Recommended Posts

It is not really that advanced what I did, just built a bit on top of what you were doing. Feel free to remove things from my code if they're too "fancy".

Have you tried playing with my code a bit to see if it works? It should do more or less what you want your code to do.

 

If you're using a for loop with no counter or stopping condition what you really want is the while loop.

I personally don't care for using goto at all, so I avoid it, as it's easy to mess up things with it.

 

Hope you get it to work properly.

Share this post


Link to post
Share on other sites

// on structures
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct Students {
int ID;
char name[80];
double GPA;
char email [1000];
};
int main()
{
   loop:{
   Students nour = {
    9103614,
     "Nour Eldin Ali",
     3.81,
     "[email protected]"
   };
   Students adham = {
   9103615,
   "Adham Ayman Zaki",
   3.91,
   "[email protected]"
   };
long ID;
cout<<"Enter the student's ID "<<endl;
cin>>ID;


 if (ID==9103614)
  cout<<"\n"<<nour.name<<"\n"<<nour.GPA<<"\n"<<nour.email<<endl;
else  if (ID==9103615)
  cout<<"\n"<<adham.name<<"\n"<<adham.GPA<<"\n"<<nour.email<<endl;
  else cout<<"\nIncorrect ID"<<endl;
   }
   char choice[50];

while( true )
{
cout << " Would you like to do this again?" << endl;
cin >> choice;
if (choice == "yes")
  cout<<"it worked";
if (choice == "no" || choice == "No" || choice == "N")
  break;
else if ( choice != "yes" || choice != "Yes" || choice != "Y")
{
  cout << "invalid choice, program terminates now" << endl;
  return 0;
}
}
   stop:
  cin.get();

}

ok i tried your way , i did not say it is complicated but why do it like this when you can do it with goto , and what is wrong with goto ..?

one more thing , note the changes i have done , when i go to the part it says do you want to do it again , ../

when i type "yes" it says invalid choice program terminates now ..

it is the same thing with the 1st code.

is this because of the compiler or what..?

Share this post


Link to post
Share on other sites

hmm weird, it never says "it worked"?I think it may have to do with some sort of basic assumption I'm making about c style strings and strings. let's try using strcmp after all. Try to see if this works:

 

 if ( strcmp(choice, "yes") == 0 )
  cout<<"it worked";
else if ( strcmp(choice, "no") == 0)
  break;
else
{
  cout << "invalid choice, program terminates now" << endl;
  return 0;
}

 

I was told a long time ago, and many times after that by various programming profs to not use goto if I could avoid it at all, and I guess I obeyed. I think there are more elegant ways to do the same things a goto does, especially in objected oriented languages. You could easily use functions and classes along with the trusty while loop to circumvent any sort of goto usage. Some of the modern programming languages go so far as to lack a goto instruction (I know java and python don't have it), so I guess there may be some sort of goto shunning conspiracy that I'm also part of. :biggrin:

Edited by PruritusAni

Share this post


Link to post
Share on other sites

and what is wrong with goto ..?

Goto leads to code which is extremely difficult to follow. If you're looking at the code a week later, it will give you a headache. More importantly, it's simply bad habit to get into when you should be learning how to properly structure code with loops/branches. Whether you're just trying to get through a class or want to be a professional software/game developer you will either a) piss off whoever is determining your grade on the programs or b) learn bad habits which will upset the other programmers on your team and make you difficult to work with (in the real world, nobody programs in isolation).

 

Also, velociraptors: http://xkcd.com/292/

 

/endgotorant

Share this post


Link to post
Share on other sites

thanks for the advice :D

back to problem :(:ouch:

i coded the program from scratch again and i will give you a pic

 

:(

i have never seen a problem that gets messy like this before

want the new code , no problem

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Students {
int ID;
char name[80];
double GPA;
char email [1000];
};
int main()
{

   char choice[10]; // for yes or no
    for (;
    {


      Students nour = {
    9103614,
     "Nour Eldin Ali",
     3.81,
     "[email protected]"
   };
   Students adham = {
   9103615,
   "Adham Ayman Zaki",
   3.91,
   "[email protected]"
   };
long ID;
cout<<"Enter the student's ID "<<endl;
cin>>ID;


 if (ID==9103614)
  cout<<"\n"<<nour.name<<"\n"<<nour.GPA<<"\n"<<nour.email<<endl;
else  if (ID==9103615)
  cout<<"\n"<<adham.name<<"\n"<<adham.GPA<<"\n"<<nour.email<<endl;
  else cout<<"\nIncorrect ID"<<endl;
  cout<<"\n if you want to do it again , type yes or no "<<endl;
  gets (choice);
    if ( strcmp(choice, "yes") == 0 )
  cout<<"\n Lets do it again ";
   else if ( strcmp(choice, "no") == 0)
  break;
else
{
  cout << "invalid choice, program terminates now" << endl;
  return 0;
}
}
cin.get();
}

guys , i know this will sound silly but ...

can some one try this code on his own compiler and see if it works , cause if it does not , this means that i am in big problem

and to check my compiler , i made a tiny program that tests the strcmp function

post-67610-12830589569523_thumb.jpg

Edited by N.E.A

Share this post


Link to post
Share on other sites

This is a very sneaky problem I've seen before that has to do with the very technical details of how gets() and extraction operator (i.e. cin >> ID) both work. For your input you are putting in something along the lines of "12345\nyes\n", where \n is a newline. In other words you entered 12345, then enter, then yes, then enter. When the extraction operator is executed it will leave the buffer pointer on the first \n because of the specific way that operation functions. When gets() runs, it reads until it sees a \n, which is immediately, so choice is getting an empty string for a value.

 

Ideally you would use the extraction operator for reading in the value of choice, but AFAIK you can't read into a char array that way. If you're able to use the string class, make choice a string and then use cin >> choice to read in it's value. If not, use gets() instead of cin >> ID read in ID (you'll have to use strcmp instead of == to compare) to fix your issue.

 

Good luck.

Share this post


Link to post
Share on other sites

Ugh, I almost forgot my hatred for c-style strings. If you have a choice, always use regular strings as they're a lot more intuitive, and you don't have to worry about buffer overflows and reading the string in properly.

 

I got this to work in my Visual Studio:

 

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

struct Students {
int ID;
char name[80];
double GPA;
char email [1000];
};

int main()
{
   char choice[256]; // for yes or no
   Students nour = { 9103614, "Nour Eldin Ali", 3.81, "[email protected]" };
   Students adham = { 9103615,"Adham Ayman Zaki", 3.91, "[email protected]" };
   long ID;


   while(true)
   {
	cout << "Enter the student's ID " << endl;
	cin >> ID;

	if ( ID == 9103614 )
		cout << endl << nour.name << endl << nour.GPA << endl << nour.email << endl << endl;
	else  if ( ID == 9103615 )
		cout << endl << adham.name << endl << adham.GPA << endl << nour.email << endl << endl;
	else 
		cout << endl << "Incorrect ID" << endl << endl;

	cout << "if you want to do it again , type yes or no " << endl;
	cin >> choice;

	if ( !strcmp(choice, "yes") )
		cout << endl << "Lets do it again!" << endl;
	else if ( !strcmp(choice, "no") )
		break;
	else
	{
		cout << endl << "invalid choice, program terminates now" << endl;
		return 0;			
	}
   }

   return 0;
}

 

it turns out that gets, or cin.getline(choice, 10) both fail so cin was the only way to make it work. It is very important to note however that if the guy enters smth with more than 9 characters you'll get buffer overflow and are setting your program up for easy hijacking. I made the string large enough that hopefully someone would get bored typing that many characters in, but this won't deter any buffer overflow attacks. Bottom line? don't use c-style strings.

 

I know there must be a way to make this work with c-style strings, but I just don't know it. Surely there must be some c-style string expert lurking around here laughing at my incompetence.

Edited by PruritusAni

Share this post


Link to post
Share on other sites

Ugh, I almost forgot my hatred for c-style strings. If you have a choice, always use regular strings as they're a lot more intuitive, and you don't have to worry about buffer overflows and reading the string in properly.

 

I got this to work in my Visual Studio:

 

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

struct Students {
int ID;
char name[80];
double GPA;
char email [1000];
};

int main()
{
   char choice[256]; // for yes or no
   Students nour = { 9103614, "Nour Eldin Ali", 3.81, "[email protected]" };
   Students adham = { 9103615,"Adham Ayman Zaki", 3.91, "[email protected]" };
   long ID;


   while(true)
   {
	cout << "Enter the student's ID " << endl;
	cin >> ID;

	if ( ID == 9103614 )
		cout << endl << nour.name << endl << nour.GPA << endl << nour.email << endl << endl;
	else  if ( ID == 9103615 )
		cout << endl << adham.name << endl << adham.GPA << endl << nour.email << endl << endl;
	else 
		cout << endl << "Incorrect ID" << endl << endl;

	cout << "if you want to do it again , type yes or no " << endl;
	cin >> choice;

	if ( !strcmp(choice, "yes") )
		cout << endl << "Lets do it again!" << endl;
	else if ( !strcmp(choice, "no") )
		break;
	else
	{
		cout << endl << "invalid choice, program terminates now" << endl;
		return 0;			
	}
   }

   return 0;
}

 

it turns out that gets, or cin.getline(choice, 10) both fail so cin was the only way to make it work. It is very important to note however that if the guy enters smth with more than 9 characters you'll get buffer overflow and are setting your program up for easy hijacking. I made the string large enough that hopefully someone would get bored typing that many characters in, but this won't deter any buffer overflow attacks. Bottom line? don't use c-style strings.

 

I know there must be a way to make this work with c-style strings, but I just don't know it. Surely there must be some c-style string expert lurking around here laughing at my incompetence.

phew , :D thanks :D

it worked perfectly ,

but i have one question , was my string a c-style or C++ ?

i think it was C++ and, god c-style is so bad i like C++ :)

again thanks for the help guys :D

but it seems like it is crazy , using cin>> instead of gets ()

i know it works but it looks crazy till you compile & run it XD

Share this post


Link to post
Share on other sites

a C++ string is used via #include <string> and is way simpler, you don't have to worry about buffer overflow or strcmp, as you can use its operators == and != . They're basically dynamically allocated arrays of chars, so you don't have to specify lengths to start with, which simplifies things a lot.

 

I suggest you use them as often as you can instead of the c-style strings (arrays of chars with fixed size is all they are, and their functionality is lacking at best).

 

What you were using are c-style strings and the code I gave you is not very safe as it allows for buffer overflow. It's only a temporary solution until someone who knows how to deal with these p00py strings pops by and enlightens us.

 

It's really not that crazy, as you'll program in C++ more you'll become very used to what I was doing over there, and once you get to write your own classes you'll see indeed just how powerful C++ can be.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...