C++ internship program

Hey everyone,
I'm new to this forum but belong to forum.codecall.net's forums...
It's great to reach out for other resources and to meet new people and I hope I can help you guy as much as you may help me.

First off, I took a computer programming internship and my first assignment was to make a simple program that basically took whatever number the user put in and multiply it by each number before it down to one, then display the result.

for example, if the user types in 5 and presses enter the program will do the following: 5*4*3*2*1=

that part of the program is complete, but I would like to add an if else statement that says:

If the users input is a letter, display a message that says "this is not a number. Please enter a number". else if the users input is a number, continue with the program...

from what i just typed it seems I need an if/else (or else if) statement?
the only thing is that I dont know what the syntax is that represents all letters or how I would set letters a-z to an integer.

is what im saying making sense? I hope Im explaining it clear enough...

here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdAfx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>
#include <ctype.h>

using namespace std;

int main ()
{

int origNumb;

	cout << "This is a program that will take the number that you enter\n";
	cout << "and multiply it, one at a time, by each number before it\n";
	cout << "until it reaches 1.\n";
	cout << "\n";
	cout << "Please enter a number: ";
		cin >> origNumb;

int totalNumb = origNumb;

while (origNumb > 1)
	{
origNumb -= 1;
totalNumb *= origNumb;	
	}

	cout << totalNumb;

system ("PAUSE");
}


can someone please help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// lloop multiplyer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <string.h>
#include <iostream>




int main()
{
	//usings
	using namespace std;

	//strings
	int a;
	int b;

	cout << "Please enter a number to start counting down from\n";
	cout << "The number will serve as a starting point for the count down\n\t\t\t====> ";
	cin >> a;
	cout << "The counting will start with\n\t\t\t====> " << a << endl;
	cout << "Press the 'ENTER' key when you are ready";
	
	//cin.get() is the alternitive to system("pause")
	//And most would say it's the only "correct" way to pause
	//In my opinion... It's all opinions.... 
	
	//This will clear all the text from the code ^up there^
	system("cls");

	cout << "The counting will start with\n\t\t\t====> " << a;
	cout << "\n\n\n";
	
	//This can not be inside of the loop, or a will reset
	b=a;

	//The start of the while loop
	while (a > 1)
	{
		
		cout << a << "\n";
		a--;
		b=(b*a);
	}
	//The code will leave out the showing of the number 1, so we'll add it back
	cout << "1";
	cout << "\n\n\tThe total is====> " << b;
	
	cout << "\n\nPress any key to exit Program\n";
	cin.get();
	return 0;
}
lol
I read but through looking for error and found one. I was gonna string it up.... When I get back from my lady's Parents house I'll fix this.... lol Sorry
no problem thanks...however, I just need to know how to go about the if/else if statement.

if the user enters a letter I need the program to say that its not a number and is an incorrect choice and wait until the user enters a number, if the user enters a number, it needs to continue with the program.

any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "stdAfx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <Windows.h>

using namespace std;

int main ()
{

int origNumb;

	cout << "This is a program that will take the number that you enter\n";
	cout << "and multiply it, one at a time, by each number before it\n";
	cout << "until it reaches 1.\n";
	cout << "\n";
	Sleep(4000);
	cout << "Please enter a number: ";
		cin >> origNumb;

int totalNumb = origNumb;

while (origNumb > 1)
	{
origNumb -= 1;
totalNumb *= origNumb;	
	}

char letters[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
					'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
					's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

if (origNumb == letters[26])
{
	cout << "You have not entered a number...";
	Sleep(300);
	cout << "Please enter a number: ";
}else

	cout << "The answer is: ";
	cout << totalNumb;
	cout << "\n";

system ("PAUSE");
}


this is what I did/added/edited to improve the code...however, it gives me this error:

The variable 'letters' is being used without being initialized...so that means I have to set it to something?
Last edited on
Just check if cin failed to parse the stream into an int.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>

int main(int argc, char* argv[])
{
	int num = 0;
	while(true) {
		std::cout << "Please enter an integer: ";
		std::cin >> num;
		if(std::cin.fail()) {
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			std::cout << "That was not an integer." << std::endl;
		}
	}
	return 0;
}

Ok that sort of makes sense...Branflakes91093, can you explain that to me?
1
2
3
4
5
6
7
8
9
while(true) {
	std::cout << "Please enter an integer: ";
	std::cin >> num; //try to get a number
	if(std::cin.fail()) { //.fail() will give us true if the stream broke, like someone entered 'a' in this case
		std::cin.clear(); //clear the error flags
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //eats the rest of the line (the junk we don't want)
		std::cout << "That was not an integer." << std::endl;
	}
}

Topic archived. No new replies allowed.