# c++ if else help



## Asylum (Dec 6, 2012)

I need the program to ask each question seperately.
What do I need to add to my code?
Please help...Thanks

```
//Your Name Here
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Sample variable data types
	int test = 0;
	int clas = 0;
	int thanksgiving = 0;
	

//Place your code here...


	cout<<"\t\t\t ******************************"<<endl;
	cout<<"\t\t\t *     Kelvin Holland         *"<<endl;
	cout<<"\t\t\t *        CPT-168             *"<<endl;
	cout<<"\t\t\t *         Exam               *"<<endl;
	cout<<"\t\t\t ******************************"<<endl<<endl;

	cout<<"Did you study for this test?";
	cin>>test;

	if(test == 'y')
	{
	cout<<"Good for you"<<endl<<endl;
	}
	
	else
	{
	cout<<"Good luck then"<<endl<<endl;
	}
	

	cout<<"Have you enjoyed this class?"<<endl<<endl;
	cin>>clas;

	if(clas == 'y')
	{
	cout<<"That is nice"<<endl<<endl;
	}
	
	else
	{
	cout<<"You should have answered Yes"<<endl<<endl;
	}

	cout<<"Did you have a good Thanksgiving?"<<endl<<endl;
	cin>>thanksgiving;

	if(thanksgiving == 'y')
	{
	cout<<"Great!"<<endl<<endl;
	}
	
	else
	{
	cout<<"Great!"<<endl<<endl;
	}





	
      


//Sample Screen Output
	cout<<"\t\t Have a Merry Christmas and a Happy New Year!!!"<<endl<<endl;


	
	system("color F0");
	system("pause");
	return 0;
}
```


----------



## 3870x2 (Dec 6, 2012)

It seems from the code that it does.  Care to explain?  Like are you wanting the screen to be cleared after each question?


----------



## MrSeanKon (Dec 7, 2012)

I cannot understand why you declared three integer variables but they store characters.... 
One the other hand cout, cin etc......
They suck! 
Use printf, getchar instead 
Anyway I modded your code and runs OK under Visual C++ 2010. Do the rest moddifications.


```
#include <iostream>
#include <iomanip>
using namespace std;

void main(void)
{
	char test,classroom,thanksgiving;   
	
	printf("\t\t\t ******************************\n");          
	printf("\t\t\t *     Kelvin Holland         *\n");
	printf("\t\t\t *        CPT-168             *\n");
	printf("\t\t\t *         Exam                *\n");
	printf("\t\t\t ******************************\n\n\n");

	cout<<"Did you study for this test? ";
	cin>>test;
	if (test == 'y')   // All variables must be characters!!!!
		cout<<"Good for you"<<endl<<endl;
	else
		cout<<"Good luck then"<<endl<<endl;
	
	cout<<"Have you enjoyed this class? ";
	cin>>classroom;
	if (classroom == 'y')
		cout<<"That is nice"<<endl<<endl;
	else
		cout<<"You should have answered Yes"<<endl<<endl;
	
	cout<<"Did you have a good Thanksgiving? ";
	cin>>thanksgiving;
	if (thanksgiving == 'y')
		cout<<"Great!"<<endl<<endl;
	else
		cout<<"Great!"<<endl<<endl;
	
	cout<<"\t\t        Anna loves me too much!!!"<<endl<<endl;
	system("color F0");
	system("pause");
}
```

Moreover no reason to use {, }.
You must use them in compound statements.
Example:


```
for (int i=1; i<=10; i++)
{
    k=i*i;
    if (k > 8)
      printf("Hello world");
    else
       printf("Damn!");
}
```


----------



## SIGSEGV (Dec 7, 2012)

MrSeanKon said:


> I cannot understand why you declared three integer variables but they store characters....
> One the other hand cout, cin etc......
> They suck!
> Use printf, getchar instead
> Anyway I modded your code and runs OK under Visual C++ 2010. Do the rest moddifications.



you said that cout or/and cin is (are) sucks compared with printf, can you explain that? 



MrSeanKon said:


> Moreover no reason to use {, }.
> You should use them in compound statements.



fixed


----------



## MrSeanKon (Dec 7, 2012)

First of all, I am not a programmer. Basically I am a hobbyist.
I started programming in pure ANSI C (scanf, printf, getchar etc) 20 years ago reading *C step by step* by Waite Group and I did not program in C++.
That's why I am not familiiar with cin, cout etc and posted that. 
Of course cout, cin do the same work. It is up to everyone which commands he/she prefers.
Todays I program in C#. I prefer this:


```
public class Example
{
   public static void Main()
   {
      printf("With the default new line characters:\n");
   }
}
```

instead of:


```
public class Example
{
   public static void Main()
   {
      System.Console.WriteLine("With the default new line characters:");
      Console.WriteLine();      
   }
}
```

Because C is a subset (which means that any C command is supported by C++, C#) I cannot understand why they developed equivalent (OK not at all) commands in them....


----------



## Asylum (Dec 7, 2012)

OH...So I just needed to declare char for my variables.

Thanks.


----------



## GSquadron (Dec 7, 2012)

Replace the integer with char on your code and it will perfectly work:
char test;
char clas;
char thanksgiving;

As for the cout to be replaced with prinf, I was 'terribly' amazed.
Only thing i will tell you about the difference is a simple example

```
printf(a, %d); // dates in 1985 C style
cout << a; // dates in 1991 C++ style
```

cout is intelligent and it will find your strings, but printf, will not.
cout has a more powerful library with a lot of functions bundled which
you will find extremely important during your C++ journey

Also, remove that system('pause'); since it dates back in C journey
and it will consume so much computer power you can't even imagine.
Replace it with cin.get(); in all your projects.
If you have used cin before to get a variable, like in your example, 
you will need to add 2x cin.get();
which means:
cin.get();
cin.get();
return 0;

In c++98, return 0 has been removed since the compiler will know when the main function will end.
So you can remove it and save time

I know one last method, which is the most important one, but i will not reveal at this stage where you are.


----------



## MrSeanKon (Dec 7, 2012)

Aleksander Dishnica said:


> As for the cout to be replaced with prinf, I was 'terribly' amazed.


Our friend would to create a simple program and result matters instead of using ANSI C or C++ statements.
Of course scanf & printf can read and write strings. But it is a beyond of my scope to post more code about this => off topic.


----------



## GSquadron (Dec 7, 2012)

One thing i was amazed even more, was that line of code:
#include <iomanip>

I am amazed by same programmers, even professionals of adding more includes than needed.
Can you explain pls why did you use that include at first? And where did you get that?


----------



## de.das.dude (Dec 7, 2012)

MrSeanKon said:


> Use printf, getchar instead



umm... cout cin is best suited for beiginers and IT IS C++, not C 

to be honest i never required the use of printf or getchar when doing C++


----------



## de.das.dude (Dec 7, 2012)

Aleksander Dishnica said:


> One thing i was amazed even more, was that line of code:
> #include <iomanip>
> 
> I am amazed by same programmers, even professionals of adding more includes than needed.
> Can you explain pls why did you use that include at first? And where did you get that?



lack of basic knowledge about what functions the library files actually contain.


----------



## librin.so.1 (Dec 7, 2012)

Aleksander Dishnica said:


> As for the cout to be replaced with prinf, I was 'terribly' amazed.
> Only thing i will tell you about the difference is a simple example
> 
> ```
> ...



There were things that I could not do with cout, but could easly do with printf. I suspect one of those things was not doable with cout was because cout was _too intelligent_ to blindly do what asked to do.
Also, printf looks much better when one needs to print out a lot of stuff. Formatting complicated output with a lot of variables and stuff requires a sh*tload of "<<" operators, so it looks horrible and is hard to edit... "f*** it, I'll just use printf"


----------



## MrSeanKon (Dec 7, 2012)

Thanks Vinska for your post. I would type something like that but I did not it (I explained above the reasons).
Asylum focus to the photos. Both are simple codes. But Visual Basic (as Pascal) protect you from run time bugs avoiding compilation. Simply, Visual Basic does not allow to compare different types.
Therefore be very careful with C++ freedom


----------



## GSquadron (Dec 8, 2012)

never mind guys, they both have their pros and cons, that is sure
i was amazed, since i read in Stephen Prata's book, to shift to cout
and i have been using cout for like 1.5 years


----------

