I'm running into problems with my local debugger on visual studio while trying to learn C++, as soon as it gets to the end of the code it immediately closes and doesn't allow me to close it myself so I can't even see the final bit of output.
As far as I know there are no errors in my code and I tried the getchar() so that I could enter a character before the debugger closing.
Here's my code just incase there are errors but I don't think there are
#include <iostream>
using namespace std;
int search(char* pchs, int size, char key) {
int count = 0;
for (int i = 0; i < size; i++) {
if (pchs[i] == key)
count++;
}
return count;
}
int main() {
int size = 0;
char key = 0;
cout << "Please enter the size of the array" << endl;
cin >> size;
cout << "Please enter the key (a-z)" << endl;
cin >> key;
char* pchs = new char[size];
if (pchs != NULL) {
for (int i = 0; i < size; i++) {
pchs[i] = 97 + rand() % 26;
cout << pchs[i] << " ";
}
cout << endl;
cout << "No. Occurences: " << search(pchs, size, key) << endl;
}
delete[] pchs;
return 0;
}
Go to Tools -> Options and search for this option and disable it
You could try this method:
Right click on your project
Properties -> Configuration Properties -> Linker -> System -> Select Console (/SUBSYSTEM:CONSOLE)
Related
I have to let user fill an array with 0-500 values, and at any moment they can exit entering the array with two consecutive empty value inputs (hitting enter twice).
Then the array displays, then it sorts itself, and finally displays again.
I have no idea how to capture enter twice in a row to exit the loop.
The best I can do to help communicate what I am doing, I used getch(); which I am pretty sure my teacher would not like since it isn't standard.
I've tried cin.peek and cin.get but if there is nothing in the stream it always waits for enter which I don't want happening.
I found getch(); and it basically bypasses the problem I am having. I need to make this program operate the same way but using something that is in the c++ standard. Hitting enter after every entry is fine, but I need to detect an empty line and then go into a state where I can detect another one. If it detects one empty line I need to be able to resume entering values.
Here is a somewhat working version with getch(); of what I am going for.
#include <iostream>
#include <conio.h>
using std::cin;
using std::cout;
using std::endl;
const int ARRAY = 500;
int GetUserArray(int b[ARRAY]);
void DisplayVals(int ArrayofValues[ARRAY], int &x);
void SortArray(int ArrayofValues[ARRAY], int& x);
int main()
{
int UserArray[ARRAY]= {0};
int NumberofValues = 0;
//User Populates Array, catches number of entries with return
NumberofValues = GetUserArray(UserArray);
//Displays the NON-SORTED array
DisplayVals(UserArray, NumberofValues);
//Sorts Array
SortArray(UserArray, NumberofValues);
//Displays the SORTED array
DisplayVals(UserArray, NumberofValues);
return 0;
}
int GetUserArray(int UserArray[ARRAY])
{
int howmany = 0;
int input = 0;
//ASCII code for enter key is 13 '\n'
int x = 0;
cout << "Please enter an integer values: ";
for (int i = 0; i < ARRAY; i++)
{
input = _getche();
if (input == 13)
{
cout << endl;
cout << "Please enter an integer value: ";
input = _getche();
if (input == 13)
{
i += 499;
cout << endl;
cout << endl;
cout << "Exiting...";
}
}
UserArray[i] = input;
howmany++;
}
return howmany;
}
void DisplayVals(int ArrayofValues[ARRAY], int &x)
{
cout << '\n' << endl;
for (int i = 0; i < x ; i++)
{
char Display = 0;
Display = ArrayofValues[i];
cout << Display << ' ';
}
cout << endl;
}
void SortArray(int ArrayofValues[ARRAY], int& x)
{
bool sorted = false;
for (int pass = 0; pass < x; pass++)
{
for (int i = 1; i < (x - pass - 1); i++)
{
if ((ArrayofValues[i - 1]) < ArrayofValues[i])
{
int temp = ArrayofValues[i - 1];
ArrayofValues[i - 1] = ArrayofValues[i];
ArrayofValues[i] = temp;
}
}
}
}
Note: ASCII value of \n is 10.
Following is the tested and working code:
int GetUserArray(int UserArray[ARRAY]) {
int input = 0;
int exit_flag = 0;
int howmany = 0;
cout << "Please enter an integer values: ";
for (int i = 0; i < ARRAY; i++) {
input = cin.get(); //Stores ASCII value inside input
if(input==10){
if(exit_flag){
i+=ARRAY; // You should not hard-code any values.
cout<<"Exiting";
}
exit_flag = 1;
--i; //If you don't want to skip an index in case of Single Enter.
continue;
}
else{
exit_flag=0;
UserArray[i] = input-'0'; //Converting ASCII to INT.
howmany++;
cin.ignore(); //Clearing the input buffer as we have pressed 'Enter'.
}
}
return howmany;
}
Thank you.
So this is for a lab assignment and I already have it working, but one thing is bothering me. The assignment involves creating a 1-dimensional array and then manipulating it. I am supposed to allow a max of 100 inputs but the user does not have to use all 100. Right now, I am using a while statement to either break or allow another input to be entered. To break the statement, you have to enter a negative number (this is what I don't like and want to change). What other options are there to end the user input, once they are done entering their numbers? Is it possible to end the loop once you hit enter with nothing typed?
I have searched stackoverflow for the last 3 days and found some compelling stuff but could never get it to work.
Note, I get the void function is redundant here but that's besides the point (unless it actually affects my ability to achieve what I want).
Also, thanks in advance.
here is my code so far (my while statement is in the main)... be kind I'm a newbie to coding.
#include <iostream>
using namespace std;
void reverseElements(int array[], int size)
{
int tmp;
int j;
int i = size;
j = i - 1;
i = 0;
while (i < j)
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
cout << "I will now reverse the elements of the array." << endl;
cout << endl;
for (i = 0; i < size; i++)
{
cout << array[i] << " " << endl;
}
}
int main()
{
const int NUM_ELEMENTS = 100;
int iArr[NUM_ELEMENTS];
int i;
int myInput;
cout << "Enter your numbers, then enter a negative number to finish" << endl;
cout << endl;
for (i = 0; i < NUM_ELEMENTS; i++) //loop to obtain input
{
cin >> myInput;
if (myInput < 0) //checks for negative number to end loop
{
break;
}
else //continues to allow input
{
iArr[i] = myInput;
}
}
cout << endl;
reverseElements(iArr, i);
return 0;
}
Probably the easiest solution: let your user choose how many numbers to write before actually writing them.
int readNumbersCount()
{
int const numbersMin = 1;
int const numbersMax = 100;
int numbersCount = -1;
while (numbersCount < numbersMin || numbersCount > numbersMax)
{
std::cout <<
"How many numbers are you going to enter? Choose from " <<
numbersMin << " to " << numbersMax << ":\n";
std::cin >> numbersCount;
}
return numbersCount;
}
int main()
{
int const numbersCount = readNumbersCount();
for (int i = 0; i < numbersCount; ++i)
{
// read the numbers etc.
}
return 0;
}
I wrote readNumbersCount() as a separate function to extract numbersMin and other "one-use" identifiers from main() and to make main()'s numbersCount const.
I have edited the main function a little bit.
Here the user is asked how many elements he wants to enter .
and doing the memory allocation dynamically so as to save space
int main()
{ int n=101;
while(n>100){
cout<<"How many numbers do you want to enter";
cin>>n;
}
int *ptr=new(nothrow)int[n];
for (int i=0;i<n;i++){
cout << "Enter your number" << endl;
cin>>ptr[i];
}
cout << endl;
reverseElements(ptr, n);
return 0;
}
I am messing around with dynamic arrays for a user defined amount of inputs for an and gate.
The issue I am running into is that I don't know how many inputs the user is going to test and I need to be able to have an if-else statement that tests each input.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput = 0;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1 = 0;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
return 0;
}
Here is the code that I am currently trying to find a solution for.
To implement an AND gate with n inputs you can simply do:
int output = 1;
for (int i = 0; i < n; ++i)
{
if (!and_gate [i])
{
output = 0;
break;
}
}
// ...
Use Vector data structure, you don't need to tell its size while declaring, unlike array, and it can grow automatically.
To read input till it's arriving, put cin inside while loop condition. I used getline to read whole line and work with it, so that whenever user presses enter button at empty line, program will think that no more input is coming anymore, and will start calculating 'And' of inputs.
//don't forget to import vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
logic_gate(){ //default constructor
}
logic_gate(int k){ //another constructor needed
x = k;
}
};
int main(){
cout << endl << "Please enter the values of each bit below . . ." << endl;
vector<logic_gate> and_gate; //no need to tell size while declaration
string b;
while(getline(cin, b)){ //read whole line from standard input
if (b == "\0") //input is NULL
break;
and_gate.push_back(logic_gate(stoi(b))); //to convert string to integer
}
if (!and_gate.empty()){
int output = and_gate[0].x;
for (int i = 1; i < and_gate.size(); i++){
output = output & and_gate[i].x;
}
cout << "And of inputs is: " << output << endl;
}
else{
cout << "No input was given!\n";
}
return 0;
}
Feel free to ask if some doubts linger
I figured out what I wanted to do. Thanks to everyone who helped and especially Paul Sanders. Below is my final code.
#include <iostream>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput;
int output = 1;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
if (userInput == 1) {
output = userTest1;
cout << "The test of " << userTest1 << " is " << output << endl << endl;
}
else if (userInput > 1) {
for (int i = 0; i < userInput; i++) {
if (!and_gate[i].x)
{
output = 0;
break;
}
}
cout << "The test of ";
for (int i = 0; i < userInput; i++) {
cout << and_gate[i].x;
}
cout << " is " << output << endl << 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 <
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)"