So basically I created this calender and when it outputs you can briefly see that it's right but it just closes down, can someone help me to keep it open to display the output?
I know I should know what this is but this took me a long time to do and my mind just isn't in the right place. thanks
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include "float.h"
#include <stack>
using namespace std;
using std::stack;
int calendar[6][7];
void cal(int y, int z) // y is number of days and z is the number corresponding
{ // to the first day
int n = 1;
for (int j = z - 1; j<7; j++)
{
calendar[0][j] = n;
n++;
}
for (int i = 1; i<6 && n <= y; i++)
{
for (int k = 0; k<7 && n <= y; k++)
{
calendar[i][k] = n;
n++;
}
}
}
int main()
{
int d;
int day;
cout << "Enter number of days : ";
cin >> d;
cout << "Enter first day of the month(1 for monday 7 for sunday..) : ";
cin >> day; cout << "\n";
cal(d, day);
cout << "M T W T F S S" << endl;
cout << "\n";
for (int i = 0; i<6; i++)
{
for (int j = 0; j<7; j++)
{
cout << calendar[i][j] << "\t";
}
cout << "" << endl;
}
}
Use std::cin.get() to keep the window open.
Since noone mentioned it - you can also do system("pause") on the Windows system family. It does what you need in the most 'elegant' way.
The easiest way is probably to ask for some input right before the end of main:
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
std::cin.ignore();
This will wait for the enter key to be hit: since there was just some numeric input done, there is still, at least, a newline in the input buffer. The first line gets rid of any already entered line. The second line then waits for the enter key.
I usually just use getchar() to wait for user input, which is shorter than std::cin.get().
Related
Question :
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
Example
Input:
1
2
88
42
99
Output:
1
2
88
So that is the question, however i am still a beginner and unable to have an input tab like that. In my program, how should i modify it such that it still accepts numbers after 42, however, it does not print them? currently I am only able to terminate the input at 42.
#include <iostream>
using namespace std;
int main()
{
int A[100], num, i=0,k,count;
for(count = 0; count != 1;){
cin >> k;
if (k!=42){
A[i] = k;
i++;
}
else
count =1;
}
cout << endl;
for (count = 0; count <i; count ++){
cout << A[count] << endl;
}
}
You don't have to use array at all. You can print the value just after reading it. Exit when you read 42. This may help you.
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n ;
for(; ;) {
cin >> n ;
if(n == 42) {
return 0 ;
}
cout << n << endl ;
}
return 0;
}
Pretty sure the easiest way to do so is to simply ask the user how many numbers they need to enter.
#include <iostream>
using namespace std;
int main()
{
int A[100], k, count;
cout << "How many numbers do you want to enter ? ";
cin >> count; //this is to count how many numbers the user wants to enter
for(int i(0); i < count; ++i) //put all the numbers user enters in your array
{
cin >> k;
A[i] = k;
}
cout << endl;
for (int i(0); i < count; ++i)
{
if (A[i] == 42) //if the element at index i is == 42 then stop displaying the elements
break;
else
cout << A[i] << " "; //else display the element
}
cout << endl;
return 0;
}
Else you would need to put everything in a string and parse it and i'm not quite sure how that goes as I am a beginner as well.
EDIT:
Actually here you go, I think that is correct and does exactly what you want.
Do keep in mind that if user enters p.e "1 88 442" it will output "1 88 4" because it found "42" in "442". But it should be okay because you precised input numbers should only be two digits max.
#include <iostream>
using namespace std;
int main()
{
string k;
getline(cin, k);
cout << endl;
for (unsigned int i(0); i < k.length(); ++i)
{
if (!((k[i] == '4') && (k[i+1] == '2'))) //if NOT 4 followed by 2 then display
cout << k[i];
else
break; //else gtfo
}
cout << endl;
return 0;
}
Use a bool value to control the execution of your code.
#include <iostream>
#define N_INPUT 100
#define THE_ANSWER 42
using namespace std;
int main()
{
int array[N_INPUT], i, input, count=0;
bool universeAnswered = false;
for (i = 0; i < N_INPUT; i++) {
cin >> input;
if (!universeAnswered)
{
if (input == THE_ANSWER) {
universeAnswered = true;
} else {
array[count] = input;
count++;
}
}
}
for (i = 0; i < count; i++) {
cout << array[i] << endl;
}
}
(My code was not tested)
You just have to have some state to see if you have seen 42 already, and only output if you haven't
#include <iostream>
int main()
{
bool output = true;
for (int n; std::cin >> n;)
{
output &= (n != 42);
if (output)
{
std::cout << n << std::endl;
}
}
return 0;
}
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int i = 0;
int j = 0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
while (i <= arrowBaseHeight){
while (j <= arrowBaseWidth){
cout << "*";
j++;
}
cout << endl;
j = 0;
i++;
}
// Draw arrow head (width = 4)
return 0;
}
I am trying to write a simple program that takes 3 user entered integers and assigns them to arrowBaseHeight, arrowBaseWidth, and arrowHeadWidth. The output should be a series of asterisks (*) that print out like:
**
**
**
****
***
**
*
to create an image of an arrow.
I have been trying to figure out the best way to print out the base portion of the arrow using nested loops (I have been using while but if for is better, let me know). I have tried multiple different ways and I have yet to figure one out that doesn't throw back an error. I have yet to get to the arrow head portion but if anyone wants to point me in the right direction, it would be helpful!
You were close, but if you want for a loop to be executed exactly n times, starting your counter i at 0, the condition should be i < n, not i <= n.
About the head, you just have to decrement the number of characters printed in every line, starting from the inputted width.
#include <iostream>
int main()
{
using std::cout;
using std::cin;
int arrowBaseHeight = 0;
cout << "Enter arrow base height:\n";
cin >> arrowBaseHeight;
int arrowBaseWidth = 0;
cout << "Enter arrow base width:\n";
cin >> arrowBaseWidth;
int arrowHeadWidth = 0;
cout << "Enter arrow head width:\n";
cin >> arrowHeadWidth;
cout << '\n';
// Draw arrow base
for ( int i = 0; i < arrowBaseHeight; ++i )
{
for ( int j = 0; j < arrowBaseWidth; ++j )
{
cout << '*';
}
cout << '\n';
}
// Draw arrow head
for ( int i = 0, width = arrowHeadWidth; i < arrowHeadWidth; ++i, --width )
{
for ( int j = 0; j < width; ++j )
{
cout << '*';
}
cout << '\n';
}
return 0;
}
You can see a lot of repeated code, consider refactoring it using some custom functions, instead.
You should change the condition of the while loops to:
while (i < arrowBaseHeight) and
while (j < arrowBaseWidth).
And for the arrowHeadWidth you could try to get the middle of the arrowBaseHeight. Maybe like this
int r = 0;
if(i == arrowBaseHeight / 2)
{
while(r < arrowHeadWidth)
{
cout << "*";
r++;
}
}
I haven't tested it. I hope it helps.
All you need to do is to add a new variable which could indicate what are you need to print right now.
The rule is :
If: up to half of "arrowBaseHeight" iteration you need to print the base
Else: print the head and after that decrease in 1
In addition finger rule - if you are using "while" and you need to increase an iterator it always indicate that you need to use For
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int newArrowBaseWidth=0;
cout << "Enter arrow base height:" << endl;
cin >> arrowBaseHeight;
cout << "Enter arrow base width:" << endl;
cin >> arrowBaseWidth;
cout << "Enter arrow head width:" << endl;
cin >> arrowHeadWidth;
cout << endl;
// Draw arrow base
for(int i=0; i < arrowBaseHeight; i++){
newArrowBaseWidth= i < arrowBaseHeight/2 ? arrowBaseWidth : arrowHeadWidth--;
for(int j=0; j < newArrowBaseWidth; j++){
cout << "*";
}
cout << endl;
}
// Draw arrow head (width = 4)
return 0;
}
Another thing is if you want to iterate n time you need to change the condition from =< that here mean - n+1 time, to <
How do I display 3 names at a time, pausing to let the user press a key before the list continues displaying.
My code now only loops the first 3 values of the array
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
string names[10]; //size of array
for (int i = 0; i < 10; i++)
{
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++)
{
for (int j = 0; j < 3; j++)
{
cout << names[j] << endl;
}
system("pause");
}
}
I changed the answer based on your comment. Instead of sleeping we just pause and wait until user inputs anything into the keyboard. Also a note, since you're using namespace, you don't need to include std::, I decided to use it since I was unsure what way you wanted.
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
int pauseCheck = 0; //new var
string names[10]; //size of array
for (int i = 0; i < 10; i++) {
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++) {
cout << names[k] << endl;
pauseCheck++; //increments check
if (pauseCheck == 3) { //if equals 3
system("pause"); //we pause till user input
pauseCheck = 0; //reset pause check
}
}
system("pause"); //last and final pause before program ends
}
Here's another way of doing it which I think is a little more straight forward:
#include <iostream>
#include <string>
#include <iomanip>
using std::setw;
using namespace std;
int main() {
string a;
string names[10]; //size of array
for (int i = 0; i < 10; i++)
{
std::cout << "Enter name ";
std::cin >> a; //user input
names[i] = a; //assigns input to array
}
cout << "\n";
for (int k = 0; k < 10; k++)
{
cout << names[k] << endl;
if((k + 1) % 3 == 0) // everytime k + 1 is divisible by 3, let user hit a key
system("pause");
}
}
You can wait for a Enter-Press with another std::cin, just write into an garbage value.
I think other ways are not platform independent. You could ofcourse use the windows api, or unix stuff to get a key press.
I've made a simple program which is supposed to ask the user for the length of a set, fill it with numbers and find the minimal value of that set. When I run the code, program works fine until the last number of the set is entered. Console prompt keeps blinking but it doesn't react to the keyboard. The program stops at this point. I don't understand why it doesn't just stop asking for input. I'm using CodeBlocks 16.01 if that matters. Here is is the source code:
#include <iostream>
using namespace std;
int main()
{
int len;
cout << "How many elements?" << endl;
cin >> len;
int myset[len];
int temp;
cout << "Enter " << len <<" numbers: " << endl;
for (int x = 0; x < len; x++)
{
cin >> temp;
myset[x] = temp;
cout << endl;
}
int mini;
for (int i = 0; i < len; i++)
{
if (i = 0)
{
mini = myset[i];
}
else if(myset[i] < mini)
{
mini = myset[i];
}
}
cout << "Minimal value of this set: " << mini << endl;
}
You set i to 0 in the if(i = 0) line... I suppose you want to write "if (i==0)"
Okay so I am a bit stuck here with an assignment and really have no idea what to do next. The user is suppose input a series of key answers (A-D) and then have multiple students (the amount depends on the users input) input their own answers and the program should compare their answers with the answer key that was previously entered and score each student individually. Now my problem is, how do compare each student's answer with the answer key in the array? Here is my code so far
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int NumOfQ;
int NumOfS;
char TestAns[50];
string StudentNames[50];
int StudentScores[50];
char StudentAns[50];
int score[50];
cout << "Please enter the Number Of Questions\n";
cin >> NumOfQ;
cout <<"\nPlease enter your answers\n";
for(int x = 0; x < NumOfQ; x++){
cin >> TestAns[x];
}
cout <<"\nPlease enter number of students\n";
cin >> NumOfS;
cout <<"\nPlease enter Student's Names\n";
for (int s = 0; s < NumOfS; s++){
cin >> StudentNames[s];
}
for (int z = 0; z < NumOfS; z++){
cout<<"\nStudent: " << StudentNames[z] << " Please enter your test answers\n";
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
}
score[z] = 0;
for (int i = 0; i <=NumOfQ; i++){
if(TestAns[i] == StudentAns[i]){
score[z]++;
}
}
}
for(int Z = 0; Z < NumOfS; Z++){
cout <<"\Student : " << StudentNames[Z] << score[Z] << " out of " << NumOfQ << endl;
}
system("PAUSE");
return 0;
}
Scoring is very easy - you add one for every match in the answer and the key, like this:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
if (TestAns[i] == StudentAns[i]) {
score++;
}
}
You can do it with even less code if you recall that logical operations return zero for "true" and one for "false".
WARNING: This may cost you some readability points among your peers, or even some real points if your professor fails to get impressed by such brevity:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
score += (TestAns[i] == StudentAns[i]);
}
Case sensitivity is another concern: what if the answer key is c, and the user enters C? The above code would not give the student his hard-earned points, which is wrong. If you force the case of both characters to upper, mixed-case comparison would be correct:
if (toupper(TestAns[i]) == toupper(StudentAns[i])) {
score++;
}
Replace your last 'for' block with:
StudentScores[z] = 0;
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
if (TestAns[a] == StudentAns[a])
++StudentScores[z];
}