• Log in or sign up for an account here: https://www.sythe.org/login

C++ Exercises + Problem

TheGreatEscape

Active Member

Banned
Joined
Feb 4, 2011
Sythe Gold
0
Messages
147
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 :)
 
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;
}
 
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;
}
 
Runescape Gamblingrunescape gambling
Back
Top