This question already has answers here:
c++ char array input - explanation
(2 answers)
Closed 5 years ago.
When inputing the text like "School" it reads and outputs how many vowels are in the text but whenever inputing "School School" it instantly closes when pressing enter.
Been trying to see the problem for multiple lessons and cant seem to find it.
#pragma hdrstop
#pragma argsused
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <iomanip>
// Autors : Deivids Roberts Saulite
/* Masīvs no N elemmntiem satur anglu alfabēta burtus. Noteikt cik masiva
ir patskanu, kas atrodas masīva elementos ar para numuriem */
int main() {
char teikums[150]; // Masiva lielums
int i, blank, patsk;
char yesno;
do {
system("cls");
patsk = blank = 0;
cout << "Ievadi teikumu:";
cin >> teikums;
cout << "Izmantotie Patskani:";
for (i = 0; teikums[i] != '\0'; i++) {
if (teikums[i] == 'a' || teikums[i] == 'e' || teikums[i] == 'o' ||
teikums[i] == 'o' || teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' || teikums[i] == 'O' ||
teikums[i] == 'U') {
++patsk;
cout << " " << teikums[i];
}
}
cout << "\nPatskanu Skaits: " << patsk;
cout << ("\nVai velaties atkartot(Y/cits):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
}
while (tolower(yesno) != 'n');
getch();
}
The second time you read in input, you check if it's a 'y' and if not, you return out of the subroutine. Which since it's main, exits your program.
Related
I just want to read every character in a file where I put characters from A TO Z but the program prints A every time and count vowels 4 and character 25 but expectation was to printing vowels 5 and characters 26 how to fix this program fixing from last 4 hours but nothing progress?
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int i, count = 0, vowel_count = 0;
string file_name;
cout << "enter file name:";
cin >> file_name;
ifstream fin;
fin.open(file_name);
char ch;
while (!fin.eof()) {
fin.get(ch);
cout << ch;
while (fin >> ch) {
i = ch;
if ((i > 63 && i < 91) || (i > 96 && i < 123))
count++;
if (i == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowel_count++;
}
cout << "\n No. of Characters in a File : " << count;
cout << "\n No. of vowel characters in the File : " << vowel_count;
}
fin.close();
return 0;
}
You have some really minor erros in the code, which I fixed for you.
Additionally, I added a check, if the file could be opened or not. That is the problem in most cases.
Please see below:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int count = 0, vowel_count = 0;
string file_name;
cout << "\nEnter file name: ";
cin >> file_name;
ifstream fin(file_name);
if (fin) {
char ch;
while (fin.get(ch)) {
cout << ch;
if ((ch >= 'A' && ch <= 'Z') || (ch > 'a' && ch <= 'z'))
count++;
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowel_count++;
}
fin.close();
cout << "\n No. of Characters in a File : " << count;
cout << "\n No. of vowel characters in the File : " << vowel_count;
}
else std::cerr << "\n\n*** Error. Could notopen '" << file_name << "'\n\n";
return 0;
}
I'm trying to use CStrings to do miscellaneous tasks in C++, such as remove all the vowels from the name provided. However, I can't seem to figure out why I'm getting this error:
Stack around the variable "name" was corrupted.
Why is this happening?
Here is the code:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
void cStringDemo();
void stringDemo();
void removeCVowels(char myGuess[50]);
int main() {
cStringDemo();
cin.get();
cin.ignore();
}
void cStringDemo() {
char name[] = "Seth Smith";
char guess[50];
cout << "Guess my name! [First and Last, EX: Bobby Hall.]" << endl;
cin.get(guess, 20);
if (strcmp(name, guess) == 0) {
cout << "Correct!" << endl;
}
else {
cout << "Incorrect!" << endl;
}
cout << "You guessed " << guess << "." << endl;
removeCVowels(guess);
}
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(myGuess, 100, nameNoVowel);
for (int x = 0; x < 50; x++) {
if (nameNoVowel[x] == 'a' || nameNoVowel[x] == 'e' || nameNoVowel[x] == 'i' || nameNoVowel[x] == 'o' || nameNoVowel[x] == 'u' || nameNoVowel[x] == 'A' || nameNoVowel[x] == 'E' ||
nameNoVowel[x] == 'I' || nameNoVowel[x] == 'O' || nameNoVowel[x] == 'U')
{
nameNoVowel[x] = ' ';
}
}
}
This is undefined behavior:
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(myGuess, 100, nameNoVowel);
You are copying from uninitialized nameNoVowel to myGuess. You should swap the arguments of strcpy_s. Also, even if you swap the two arguments of strcpy_s, the limit of 100 is also too big, since nameNoVowel is only 50 chars. Try:
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(nameNoVowel, sizeof(nameNoVowel)-1, myGuess);
There are a few problems in the code that you posted. Below is code fixed with explanation in comments of what was wrong:
void cStringDemo() {
char name[] = "Seth Smith";
char guess[50] = {0}; //in here initialize the table with zeros
cout << "Guess my name! [First and Last, EX: Bobby Hall.]" << endl;
cin.get(guess, 20); // I am not sure why you want 20 characters and have array of size 50
if (strcmp(name, guess) == 0) {
cout << "Correct!" << endl;
}
else {
cout << "Incorrect!" << endl;
}
cout << "You guessed " << guess << "." << endl;
removeCVowels(guess);
}
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50] = {0}; //it is always good to initialize variables
strcpy_s(myGuess, 50, nameNoVowel); //here lies the problem you tried to copy 100
// characters from array size of 50 this leads
//to undefined behaviour of your program and stack corruption
for (int x = 0; x < 50; x++) {
if (nameNoVowel[x] == 'a' || nameNoVowel[x] == 'e' || nameNoVowel[x] == 'i' || nameNoVowel[x] == 'o' || nameNoVowel[x] == 'u' || nameNoVowel[x] == 'A' || nameNoVowel[x] == 'E' ||
nameNoVowel[x] == 'I' || nameNoVowel[x] == 'O' || nameNoVowel[x] == 'U')
{
nameNoVowel[x] = ' ';
}
}
}
I am working on a project right now and when I try to run what I have below it gives me an error that says "uninitialized local variable 'userOption' used" on line 22, while (isValidOption(userOption) == true) {.
How do I fix that error? Thank you.
#include<iostream>
#include <string>
using namespace std;
char toupper(char ch) {
if (ch >= 'A'&&ch <= 'Z')
return(ch);
else
return(ch - 32);
}
bool isValidOption(char ch) {
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X')
return(true);
else
return(false);
}
char getMainOption() {
string UserInput;
char userOption;
while (isValidOption(userOption) == true) {
cout << "Choose One of the following options\n";
cout << "I--List Our Inventory\n";
cout << "O--Make an Order\n";
cout << "L--List all Orders made\n";
cout << "X--Exit\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
if (!isValidOption(userOption)) {
cout << "Invalid String\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
}
if (userOption == 'I')
cout << "Listing Our Inventory\n";
else if (userOption == 'O')
cout << "Make an order\n";
else if (userOption == 'L')
cout << "Listing all orders\n";
}
return userOption;
}
int main() {
char choice;
choice = getMainOption();
system("pause");
return 0;
}
What the error is saying that you're trying to read from userOption before you've ever written to it. If a variable is uninitialized, its memory contents will be full of junk left behind by other functions and it can easily cause bugs. In your case, you'll want to read input from the user into userOption before you do any logic on it. This can be done with a do-while loop:
char userOption; // not yet initialized
do {
...
cin >> userOption; // userOption gets initialized here on first loop run
} while (isValidOption(userOption)); // no need for == true, that's a tautology :-)
// NOTE: perhaps you want to loop while the input is INvalid, as in
// while (!isValidOption(userOption)); ?
A couply code-review comments I would additionally give are:
std::toupper already exists in <cctype>. Docs are here
return is not a function call and it's better to write return ch; than return(ch);
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X'){ return true; } else { return false; } is completely equivalent to the shorter return ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X';
Also take a look at system(“pause”); - Why is it wrong?
Happy coding! Let me know if questions remain
Been given a task to change my code to dynamic array.
The program asks you to input array size 1 to 100. After that input vowels 1 by 1 with the normal array teikums[100]; it outputted the vowels and the vowel amount.
Been given a task to make dynamic array without the "new".
But after adding the dynamic array:
char *teikums = (char*)malloc(100);
It outputs blank space.
#pragma hdrstop
#pragma argsused
#include <string>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main() {
char *teikums = (char*)malloc(100);
int c, i, count, patsk; // Patskani
char yesno; // Atkartosanas Mainigais
do {
cout << " " << teikums[i];
system("cls"); // Notira Ekranu
do {
patsk = 0; // Pieskir vertibu
cout << "Input array size 1-100: ";
cin >> count;
if (count > 100 || count < 1) {
cout << "Array cant be lower or higher than 0\n";
}
} while (count > 100 || count < 1);
do {
cout << "Input " << count << "letters one by one\n";
for (i = 1; i <= count; i++) {
cin >> teikums[i];
if (!((teikums[i] >= 'a' && teikums[i] <= 'z') || (teikums[i] >=
'A' && teikums[i] <= 'Z'))) {
cout << "Error! Only input letters\n";
i = i - 1;
}
}
} while (i <= count);
cout << "\nUsed Vowels:";
for (i = 0; teikums[i] != '\0'; i = i + 2) {
if (teikums[i] == 'a' || teikums[i] == 'e' || teikums[i] == 'i' ||
teikums[i] == 'o' || teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' || teikums[i] == 'O' ||
teikums[i] == 'U') {
++patsk;
cout << " " << teikums[i];
teikums[i] = 0;
}
}
cout << "\nVowel ammount: " << patsk;
cout << ("\nDo you wish to continue(Y/Else):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
} while (tolower(yesno) != 'n');
getch();
}
In this line:
cout << " " << teikums[i];
i is not initialized and therefore it contains an indeterminate value.
This is causing all your troubles. But there may be other problems elsewhere, I didn't check all details.
If it worked with char teikums[100]; it's pure coincidence.
Google "C undefined behaviour" for more information.
Start of the program you need to input how many elements you want for example if selected 3 you can type a b c but if you input more than 3 elements a b c d the program instantly crashes.
Haven't figured out how to make if you input the more than 4 element a b c d it will only read the a b c part.
#pragma hdrstop
#pragma argsused
#include <string>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <iomanip>
#include <sstream>
int main() {
char teikums[100]; // Masiva lielums
int c, i, count, patsk; // Patskani
char yesno; // Atkartosanas Mainigais
do {
system("cls"); // Notira Ekranu
patsk = 0; // Pieskir vertibu
cout << "Ievadi Massiva lielumu 1-100: ";
cin >> count;
if (count > 100 || count < 1) {
cout << "Massivs nedriklst but lielaks par 100 vai mazaks par 0";
}
else {
cout << "Ievadi " << count << " burtus vienu pa vienam\n";
for (i = 1; i <= count; i++) {
cin >> teikums[i];
}
cout << "\nIzmantotie Patskani:";
for (i = 0; teikums[i] != '\0'; i = i + 2) {
if (teikums[i] == 'a' || teikums[i] == 'e' ||
teikums[i] == 'o' || teikums[i] == 'o' ||
teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' ||
teikums[i] == 'O' || teikums[i] == 'U') {
++patsk;
cout << teikums[i];
}
}
cout << "\nPatskanu Skaits: " << patsk;
}
cout << ("\nVai velaties atkartot(Y/cits):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
}
while (tolower(yesno) != 'n');
getch();
}
Your program will not behave as you expect if you enter in more numbers than your count because of how it is parsing the inputs.
for (i = 1; i <= count; i++) {
cin >> teikums[i];
}
Let's say your count is 1, and you enter in 1 2.
This will loop once and say teikum[1] to be 1, and 2 is still leftover waiting to be grabbed by an input stream. Below that you have,
cin >> yesno;
which will now set yesno to be 2 and your program will terminate because 2 != 'n'. You will need to either input the data correctly as your format expects or clear cin using something like cin.ignore('\n') which will ignore the current line of input.
Further, you start your i at 1 and go up to count but your array is only size 100. If count is 100, it will try to access teikum[100] which will be out of bounds as your array goes from 0 - 99