So basically I'm writing a small program that asks the suer how many numbers they wish to enter. Then if any of those numbers is equal to a 9, the program should output the position in which the first 9 was found. It should then report of the position of the last 9 entered. If there are no 9's whatsoever, it will display a no 9's entered messsage. This is the output I'm trying to get. Basically, I've gone blank on how to approach finding the position. Very new at C++.
How many numbers will be entered? 8
Enter num: 5
Enter num: 9
Enter num: 6
Enter num: 9
Enter num: 9
Enter num: 3
Enter num: 8
Enter num: 6
The first 9 was in position 2
The last 9 was in position 5
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int num;
int answer;
int count = 0;
int firstPosition =0 ;
int lastPosition;
cout << "How many numbers will be entered? ";
cin >> answer;
for (count =0; answer > count; count++)
{
cout << "Enter number: ";
cin >> num;
}
//I'm just using these to test if there is a 9 or not.
if (num == 9)
cout << "There is a nine";
elseif (num != 9)
cout << "no nine" << endl;
system("PAUSE");
return 0;
}
Line 25 and 28 also don't seem to be working properly. I entered a 9 which should display a "there is a nine" message, instead I get a "no nine" message.
for (count =0; answer > count; count++)
{
cout << "Enter number: ";
cin >> num;
if (num == 9) {
if (firstPosition == 0) firstPosition = count;
else lastPosition = count;
}
}
cout<<"The first 9 was in position "<<firstPosition<<endl;
cout<<"The last 9 was in position "<<lastPosition<<endl;
Thanks eryar, it's those darn '}'s that also keep messing me up. That also doesn't seem to working however, but I think I found a way. I'm still having trouble getting the lastPosition, any help?
1 2 3 4 5 6 7 8 9 10 11 12
cout << "How many numbers will be entered? ";
cin >> answer;
for (count =0; answer > count; count++)
{
cout << "Enter number: ";
cin >> num;
positionCount++;
if (num == 9)
first9Position = positionCount;
Thanks eryar, it's those darn '}'s that also keep messing me up. That also doesn't seem to working however, but I think I found a way. I'm still having trouble getting the lastPosition, any help?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cout << "How many numbers will be entered? ";
cin >> answer;
for (count =1; answer >= count; count++)
{
cout << "Enter number: ";
cin >> num;
positionCount++;
if (num == 9) {
if (!first9Position) first9Position = positionCount;
else last9Position = positionCount; // here you can find the last 9 position
}
Because the data were not store in an array or other format, so the find procedure must be done after enter numbers.
When a 9 appears, decide whether it is the first9Position, if so use first9Position to record it, else use last9Position to record it. So the first9Position will only record once, but the last9Position will record every 9 appears except the first 9. After the input finished, the last9Position will record the last 9 position.
//------------------------------------------------------------------------------
// Copyright (c) 2009 eryar All rights reserved.
//
// File : Main.cpp
// Author : eryar@163.com
// Date : 2009-10-11 0:17
// Version : 1.0v
//
// Description :
//
//==============================================================================
#include <iostream>
usingnamespace std;
int main(int argc, char* argv[])
{
int num;
int answer;
int count = 0;
int firstPosition = 0;
int lastPosition;
cout<<"How many numbers will be entered?";
cin>>answer;
for (count = 1; answer >= count; ++count ) {
cout<<"Enter num: ";
cin>>num;
if (num == 9) {
if (!firstPosition) {
firstPosition = count;
}
else {
lastPosition = count;
}
}
}
cout<<"The first 9 was in position "<<firstPosition<<endl;
cout<<"The last 9 was in position "<<lastPosition<<endl;
return 0;
}
Thanks eryar, but the part I'm still having problems with is displaying the "No 9's found" message properly, if no 9's were entered by the user. As of now, the message will display if no 9's are not entered, but the first and last positions will also still be displayed. Which are -1, and 0. I don't know quiet where to place it.
// Copyright (c) 2009 Krypt All rights reserved.
//
// File: Main.cpp
// Author: Krypt
// Date: 2009-10-10
// Version: 1.2v
//_________________________________________________________________________
//Program asks user for number of integers to be entered. The program then looks for any 9's
//entered. If any, it displays their position value as an integer. If none are found,
//display a "No 9's" message.
#include<iostream>
usingnamespace std;
int main()
{
//Integers variables.
int firstNine=-1;
int lastNine=0;
int count;
int nums;
int number;
//Ask user to decide how many numbers will be entered.
cout << "How many numbers are going to be entered?: ";
cin >> nums;
for(count = 0;count < nums; count++)
{ {cout << "Enter number:";
cin >> number;}
if (number == 9 && firstNine < 0)
{
firstNine=count;
}
elseif (number == 9 && firstNine >= 0)
{
lastNine=count;
}
}
cout << endl;
cout << "The first 9 was in position: " << firstNine+1 << endl;
if (firstNine >= 0 && lastNine == 0)
{
cout << "The last 9 was in position: " << firstNine+1 << endl;
}
else
{
cout << "The last 9 was in position: " << lastNine+1 << endl;}
/*If number isn't 9, and no value has been assigned
to firstNine or lastNine, no 9's were entered.*/
if(number !=9 && firstNine == -1 || lastNine ==0)
{
cout << "Sorry, no nines were entered." << endl;
system("PAUSE");
return 1;}
system("PAUSE");
return 0;
}
//------------------------------------------------------------------------------
// Copyright (c) 2009 eryar All rights reserved.
//
// File : Main.cpp
// Author : eryar@163.com
// Date : 2009-10-11 0:17
// Version : 1.1v
//
// Description :
//
//==============================================================================
#include <iostream>
usingnamespace std;
int main(int argc, char* argv[])
{
int num;
int answer;
int count = 0;
int firstPosition = 0;
int lastPosition;
bool flag = false; // if no nines, toggle it to false
cout<<"How many numbers will be entered?";
cin>>answer;
for (count = 1; answer >= count; ++count ) {
cout<<"Enter num: ";
cin>>num;
if (num == 9) {
flag = true; // if nine appears, toggle it to true
if (!firstPosition) {
firstPosition = count;
lastPosition = firstPosition; // only one nine situation
}
else {
lastPosition = count;
}
}
}
if (flag) {
cout<<"The first 9 was in position "<<firstPosition<<endl;
cout<<"The last 9 was in position "<<lastPosition<<endl;
}
else {
cout<<"Sorry, no nines were entered."<<endl;
}
return 0;
}