1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

c++ if else help

Discussion in 'Programming & Webmastering' started by Asylum, Dec 6, 2012.

  1. Asylum

    Asylum

    Joined:
    Jul 18, 2008
    Messages:
    2,806 (1.23/day)
    Thanks Received:
    679
    Location:
    South Carolina
    I need the program to ask each question seperately.
    What do I need to add to my code?
    Please help...Thanks
    Code:
    //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;
    }
    
     
  2. 3870x2

    3870x2

    Joined:
    Feb 26, 2008
    Messages:
    4,875 (2.01/day)
    Thanks Received:
    689
    Location:
    Joplin, Mo
    It seems from the code that it does. Care to explain? Like are you wanting the screen to be cleared after each question?
     
  3. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    I cannot understand why you declared three integer variables but they store characters.... :rolleyes:
    One the other hand cout, cin etc......
    They suck! :p
    Use printf, getchar instead ;)
    Anyway I modded your code and runs OK under Visual C++ 2010. Do the rest moddifications.

    Code:
    #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:

    Code:
    for (int i=1; i<=10; i++)
    {
        k=i*i;
        if (k > 8)
          printf("Hello world");
        else
           printf("Damn!");
    }
    
     
    Asylum says thanks.
  4. SIGSEGV

    SIGSEGV

    Joined:
    Mar 31, 2012
    Messages:
    507 (0.54/day)
    Thanks Received:
    107
    you said that cout or/and cin is (are) sucks compared with printf, can you explain that? :)

    fixed
     
    Last edited: Dec 7, 2012
  5. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    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:

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

    Code:
    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....
     
    Last edited: Dec 7, 2012
  6. Asylum

    Asylum

    Joined:
    Jul 18, 2008
    Messages:
    2,806 (1.23/day)
    Thanks Received:
    679
    Location:
    South Carolina
    OH...So I just needed to declare char for my variables.

    Thanks.
     
  7. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.83/day)
    Thanks Received:
    304
    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
    Code:
    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.
     
    SIGSEGV says thanks.
  8. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    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.
     
    Last edited: Dec 7, 2012
  9. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.83/day)
    Thanks Received:
    304
    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?
     
  10. de.das.dude

    de.das.dude Pro Indian Modder

    Joined:
    Jun 13, 2010
    Messages:
    7,780 (4.89/day)
    Thanks Received:
    2,058
    umm... cout cin is best suited for beiginers and IT IS C++, not C :p

    to be honest i never required the use of printf or getchar when doing C++
     
  11. de.das.dude

    de.das.dude Pro Indian Modder

    Joined:
    Jun 13, 2010
    Messages:
    7,780 (4.89/day)
    Thanks Received:
    2,058
    lack of basic knowledge about what functions the library files actually contain.
     
  12. Vinska

    Vinska

    Joined:
    Jul 23, 2011
    Messages:
    1,419 (1.20/day)
    Thanks Received:
    1,258
    Location:
    Kaunas, Lithuania
    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"
     
    Crunching for Team TPU
  13. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    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 :)
     

    Attached Files:

  14. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.83/day)
    Thanks Received:
    304
    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
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page