C++ Exercises + Problem

Discussion in 'Programming General' started by TheGreatEscape, Aug 31, 2011.

C++ Exercises + Problem
  1. Unread #1 - Aug 31, 2011 at 6:17 AM
  2. TheGreatEscape
    Joined:
    Feb 4, 2011
    Posts:
    147
    Referrals:
    1
    Sythe Gold:
    0

    TheGreatEscape Active Member
    Banned

    C++ Exercises + Problem

    Im very very new to c++, and have just started out learning it.

    My Problem
    I made a very simple console application and it worked fine, but i wanted to extend on it for some fun.
    So here is what i have so far

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int main() {
    
    int n1, n2, i;
    
    
    cout << "Please Enter A Number: ";
    cin >> n1;
    cout << "Please Enter A Different Number: ";
    cin >> n2;
    
    if (n1 == n2) 
    	cout << "Numbers are the same, Learn To Read";
    else 
    	if (n1 <= n2)
    		for (i = n1; i <= n2; i++) 
    		cout << i << " ";
    	else
    		for (i = n2; i <= n1; i++)
    		cout << i << " ";
    
    return 0;
    }
    As you can see, very basic.
    but i want to change it, so at the very start, the user can choose too count backwards or forwards.
    I tried something like this

    Code:
    cout << "Would you like to count backwards? (y/n): ";
    but then i realized i have no idea how to figure out if the user typed 'y' or 'n'.

    i tried creating a char variable then cheeking if it equaled 'y'. But, it would not work.
    the next thing i thought would be using true/false, but still had no idea how to work out what the user types.

    Please Help :)

    C++ Exercises
    Since im new to c++, i really wanted a way to track my progress. As you can see above i am still a noob :(
    Anyways, i was hoping you could suggest a for 'exercises', so im able to see how far im coming along.

    For example.
    2 beginner applications
    2 intermediate applications
    2 advanced applications

    thanks :)
     
  3. Unread #2 - Sep 1, 2011 at 1:56 PM
  4. BotsInc
    Joined:
    Aug 12, 2011
    Posts:
    78
    Referrals:
    0
    Sythe Gold:
    0

    BotsInc Member

    C++ Exercises + Problem

    I suggest you get "Accelerated C++", since you know the basics.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
        int n1, n2, i;
    
    
        cout << "Please Enter A Number: \n"; //so we wouldnt have text cramped together
        cin >> n1;
        cout << "Please Enter A Different Number: \n";
        cin >> n2;
    
        if (n1 == n2)
            cout << "Numbers are the same, Learn To Read";
    
        cout << "Would you like to count backwards? (y/n): ";
    
        char input;
        cin >> input;
    
        switch(input)
        {
    
        case 'y':
            //doStuff
            break;
    
        case 'n':
            //doStuff
            break;
    
        default:
            cout << "Something went wrong...\n"; //We need not provide a default, but in this case its useful
    
        }
    
        return 0;
    }
    
     
  5. Unread #3 - Sep 2, 2011 at 6:34 AM
  6. TheGreatEscape
    Joined:
    Feb 4, 2011
    Posts:
    147
    Referrals:
    1
    Sythe Gold:
    0

    TheGreatEscape Active Member
    Banned

    C++ Exercises + Problem

    Thanks for the reply BotsInc.
    i purchased the book you recommended, hopefully it does not take too long to get here.

    Added the piece of code you gave me and it worked perfectly. thanks :)
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        int n1, n2, i;
    
    	cout << "Welcome to Nathan's Counting Game \n";
    	cout << "Please Enter A Number: \n"; 
        cin >> n1;
        cout << "Please Enter A Different Number: \n";
        cin >> n2;
    	
    
        if (n1 == n2)
            cout << "Numbers are the same, Learn To Read";
    	else
    		cout << "Would you like to count backwards? (y/n): \n";
    	char input;
        cin >> input;
    		
    		switch(input)
    		{
    
    	    case 'y':
    			if (n1 >= n2)
    				for (i = n1; i >= n2; i--)
    				cout << i << " ";
    			else
    				for (i = n2; i >= n1; i--)
    				cout << i << " ";
    			break;
    
    		case 'n':
    			if (n1 <= n2)
    				for (i = n1; i <= n2; i++) 
    				cout << i << " ";
    			else
    				for (i = n2; i <= n1; i++)
    				cout << i << " ";
    			break;
    
    	    default:
    		    cout << "Something went wrong.... \n"; 
    
        }
    
        return 0;
    }		
     
< A loader - Two problems | Simple while loop in VB.NET >

Users viewing this thread
1 guest


 
 
Adblock breaks this site