How to write a program that keeps prompting user for an integer.

Write a program that keeps prompting user for an integer. Upon an inpu

Page 1 of 1

14 Replies - 8644 Views - Last Post: 18 June 2009 - 04:12 AM Rate Topic: -----

#1 crimsonshad1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-June 07

How to write a program that keeps prompting user for an integer.

Post icon  Posted 15 June 2007 - 12:32 AM

Write a program that keeps prompting user for an integer. Upon an input of 0, the program will show the first 5 (if not, lesser) unique integers entered.

#include<iostream>
using namespacestd;

int absolute(int v); //OR use abs() predefined function from cstdlib
void printAscendingTriangle(int input);
void printPattern(int input);
void printDescendingTriangle(int input);

void main(void)
{
	int input;

	cout << "Input an integer: ";
	cin >> input;

	if(input > -3 && input < 3)
	{
		cout << "Invalid range." << endl;
	}
	else if(input % 2 == 0)
	{
		cout << "Odd integers only." << endl;
	}
	else
	{
		printAscendingTriangle(absolute(input));
		printPattern(input);
		printDescendingTriangle(absolute(input));
	}
}



can you please help me to correct this code? the sample output should be in this format shown below. thank you.

~ Sample Outputs ~

Enter integer: 1
Enter integer: 2
Enter integer: 6
Enter integer: 2
Enter integer: 1
Enter integer: 5
Enter integer: 4
Enter integer: 2
Enter integer: 7
Enter integer: 8
Enter integer: 3
Enter integer: 0
The first 5 unique values are:
1 2 6 5 4

Enter integer: 2
Enter integer: 3
Enter integer: 2
Enter integer: 3
Enter integer: 1
Enter integer: 5
Enter integer: 2
Enter integer: 3
Enter integer: 1
Enter integer: 0
The first 5 unique values are:
2 3 1 5

Enter integer: 0
No unique values!

Is This A Good Question/Topic? 0
  • +

Replies To: How to write a program that keeps prompting user for an integer.

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: How to write a program that keeps prompting user for an integer.

Posted 15 June 2007 - 01:39 AM

You will need to use a loop here.
A do...while loop would be a good choice.
You can use it like this:


int number=0;
do
{
	cout<<"Enter Number : ";
	cin>>number;
	add_number_and_check_if_unique(number);
}while(number!=0);

//You can also use 'for' or 'while' loop to implement this



You will need to write a function ( add_number_and_check_if_unique(int) in this case ) that checks if the argument is already present in the Array and if not, Add it to an Array.
Since you need to output only the first five unique elements, you can create an array of size 5.
Was This Post Helpful? 0
  • +
  • -

#3 you2ndfather  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-May 09

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 04:37 AM

View Postyou2ndfather, on 8 Jun, 2009 - 03:33 AM, said:

I want to know whether there is a very elegent user login functionality holding code has introduced or not...

Consider that it will ask 1st enter your ID and passward and then show msg of correct submittion incase of numeric ID name and character +numeric given passward. otherwise it will give hints to the user that enter numberical words for user name and passward that you have already selected.

Write correct code for the user login in c++.

Make use of Hungarian Notation for coding purpose
Make use of Comments in the Code where required necessary.

This post has been edited by you2ndfather: 08 June 2009 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 05:05 AM

Hi you2ndfather

A few suggestions.

1 - Your own words do not belong in quote tags.
Just write into the body of your message like I have here.

2 - You will find it much more successful to start your own topic thread rather than try and hijack this ancient old thread.

3 - When you do open your own topic thread you will need to be aware that:
[rules][/rules]

This post has been edited by janotte: 08 June 2009 - 05:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 06:33 AM

1-it's namespace std not namespacestd :D
2-all you need is a loop and an if statement. for example:

while(answer=="yes"){
cout<<Number: ";
cin>>number;

cout<<"Again? ";
cin>>answer;
}




3-please double check your post before submitting or edited once you found the mistakes.

hope that helps :)

This post has been edited by computerfox: 08 June 2009 - 06:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 06:42 AM

Beware of this thread computerfox

It's a necro brought back to life by you2ndfather
Was This Post Helpful? 0
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 06:44 AM

you mean this thread is old and should be closed or dead?

This post has been edited by computerfox: 08 June 2009 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 06:49 AM

View Postcomputerfox, on 8 Jun, 2009 - 05:44 AM, said:

you mean this thread is old


Yes

View Postcomputerfox, on 8 Jun, 2009 - 05:44 AM, said:

and should be closed?


No
Just left to go back to the archive.
If a Mod wanted to do something they might want to split off you2ndfather's question into a new thread but it might be just as easy to let it slide away and have you2ndfather start his/her own thread themselves.
But revisitng the 2007 content is certainly not going to move things forward.
Was This Post Helpful? 0
  • +
  • -

#9 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to write a program that keeps prompting user for an integer.

Posted 08 June 2009 - 06:54 AM

oh okay thank you for telling me.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,562
  • Joined: 09-June 09

Re: How to write a program that keeps prompting user for an integer.

Posted 09 June 2009 - 04:06 PM

use a vector, for every input the vector.resize = (vector.size()+1) or establish a counter(probably easier) , you while loop should have conditions like such:
int myNUMBER;
do{
counter++;
cin>>myNUMBER;
}while(myNUMBER!=0);

for(int i = 0; i = 5; i++)
{
cout<<vector[i]<<endl;
}


Was This Post Helpful? 0
  • +
  • -

#11 Unoriginal-Form  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-June 09

Re: How to write a program that keeps prompting user for an integer.

Posted 17 June 2009 - 08:46 AM

hello...I'm a newbie here so please take it lightly on me>_<
in any case,I'm having this question for my assignment...and I can't seem to figure out whats wrong...I didn't really understand the previous post as I'm not a customed to vectors yet....I'm still in the early learning stage of array>_<

so..back on topic..
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
void main()
{
	//declaration of integer arrays, and integer for loop of function

	int input=1;
	int value_list[9999];//assuming the asshole isn'tr eally gonna put 9999 integers>.>
	int print[5];
	
		//start of for loop, number is kept prompt with "blank" array value 
		//, input is put in this value so that it exits loops when entering "0"
		for (int loop=0; input !=0; loop++)
		{ 
			cout<<"Enter integer[0 to exit] : ";
			cin>>input;
			value_list[loop]= input;//so that the array value is set to loop for infinite ammounts, till 0 is entered
		
			
				for (int fives=0; fives<5;fives++)
				{
					int add=0;
					int addd=1;
					if ( value_list[add] != value_list[addd])
					{
						print[fives]= value_list[loop+add];
						add++;
						addd++;
					}
					else
					{
						continue;
					}
				}
		}
		for (int ploop=0; ploop<5; ploop++)
		{
			cout<<print[ploop]<<endl;
		}
		system("pause");
}


frankly...I think I got the "prompt infinitely" part down, problem is...this set of "attempt" to print 5 unique numbers gets me 5 "0"s ...And I'm unfamiliar with how to use the list::unique operator....so please help? any would be very much appreciated
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to write a program that keeps prompting user for an integer.

Posted 17 June 2009 - 08:56 AM

in my opinion you should first add a max integer then go through a loop. so something like this:

int MAX;
int num[MAX];

cout<<"Max numbers: ";
cin>>MAX;

//input
for(int i=0;i<MAX;i++){
cout<<"Number: ";
cin>>num[i];
}

//output
for(int i=0;i<MAX;i++){
cout<<"Number: "<<num[i]<<endl;
}



or you could have an if statement with a counter, but i think if you had a max it would be more efficient. if you wanted to do it this way it should be something like:


int counter=0, num[999];
char answer;

//input
cout<<"Number: ";
cin>>num[i];
cout<<"More? ";
cin>>answer;

if(answer=='y'){
counter=counter+1;
for(int i=0;answer=='y';i++){
cout<<"Number: ";
cin>>num[i];
cout<<"More? ";
cin>>answer;
counter=counter+1;
}
}

//output
for(int i=0;i<counter;i++){
cout<<"Number: "<<num[i]<<endl;
}




hope that helps :)

This post has been edited by computerfox: 17 June 2009 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,562
  • Joined: 09-June 09

Re: How to write a program that keeps prompting user for an integer.

Posted 17 June 2009 - 10:39 AM

or
int myNUMBER;
do{
counter++;
cin>>myNUMBER;
}while(myNUMBER!=0);

for(int i = 0; i = vector.size(); i++)
{
cout<<vector[i]<<endl;
}


This post has been edited by ImaSexy: 17 June 2009 - 10:39 AM

Was This Post Helpful? 0
  • +
  • -

#14 Unoriginal-Form  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-June 09

Re: How to write a program that keeps prompting user for an integer.

Posted 17 June 2009 - 09:47 PM

well, apparently I'm not actually allowed to prompt the user if they wanted to key in more integers(and frankly>.< I think its going to make my code look more confusing)
I'm rather supposed to keep prompting them to key in integers non-stop till they key "0" , in which I display 5 unique numbers or is less, the first 5 numbers...

Apparently I gave up on getting an Infinite loop for the array value and just put it at a number they really have no time to reach,so thats (more or less) finished...But could anyone teach me on how unique::list syntax works?o.O

I've been trying it out, but I can't get it to work
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to write a program that keeps prompting user for an integer.

Posted 18 June 2009 - 04:12 AM

Unoriginal-Form
This should help
http://www.cplusplus...tl/list/unique/



-----
This necro thread will not die.
Need a new name for threads that become combined messes of various people's queries. "Portmanteau", maybe?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1