Reading from an input file - c++

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
int size = 0;
int highNum = 0;
int m;
string fname;
cout << "Supply the name of the input file you would like to use." << endl;
cin >> fname;
ifstream input;
input.open(fname.c_str());
input >> size;
int numbers[size];
for (int n = 0; n < size; n++)
input >> numbers[n];
for (m = 0 ; m < size ; m++)
{
if (numbers[m] > highNum)
highNum = numbers[m];
}
int j;
int k;
bool values[] = {false, false, false, false, false, false};
for (j = highNum; j > 0 ; j--)
{
for (k = size - 1 ; k >= 0 ; k--)
{
if (j <= numbers[k])
values[k] = true;
}
if (values[0])
cout << "| xxx";
else
cout << "| ";
if (values[1])
cout << " +++";
else
cout << " ";
if (values[2])
cout << " ***";
else
cout << " ";
if (values[3])
cout << " ---";
else
cout << " ";
if (values[4])
cout << " +++";
else
cout << " ";
if (values[5])
cout << " +++" << endl;
else
cout << " " << endl;
}
return 0;
}
I am trying to write a code to print a bar chart by reading integers from a separate text file. I posted the complete code, but I know the bottom half works if you just input numbers. I was wondering what the problem with this is given that there is a data file in the same directory. When I run this program and enter the name of the data file i created to test it, the program runs, but no graph is produced.

Your program works for me. I suppose your input file isn't present in the current working directory of your process. I suggest you add a check to see if input is valid after the call to open.

Related

If statement inside of a for cycle

first of all, sorry if my title isn't appropriate. English isn't my first language so I didn't really know how to phrase it correctly and understandably.
So, I'm learning some C++ and I got stuck on a problem. Right now my code looks like this:
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i << " is divisible by ";
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
cout << j << " ";
}
}
cout << endl;
}
}
return 0;
}
And the output I get is:
Number i is divisible by j1, j2 and etc.
Number i is divisible by
Number i is divisible by j1, j2 and etc.
You get the idea.
What I'm trying to get is this
Number i is divisible by j1, j2 and etc.
Number i isn't divisible by any numbers
Number i is divisible by j1, j2 and etc.
I tried doing this:
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i;
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
cout << " is divisible by " << j << " ";
}
else
{
cout << " isn't divisible by any numbers";
}
}
cout << endl;
}
}
return 0;
}
But the output is just a mess. Sorry if this is something straight forward and something I could've looked up on the net, but I just didn't know how to phrase my problem correctly and what to look for. I'll appreciate any help.
You should
create a flag that indicates if any j that divides i.
after the loop, check the flag and print the message if applicable.
#include <iostream>
using namespace std;
int main()
{
int start, finish;
cout << "Enter range start" << endl;
cin >> start;
cout << "Enter the range end" << endl;
cin >> finish;
if (start < finish)
{
for (int i = start; i <= finish; i++)
{
cout << "Number " << i;
bool divisorFound = false;
for (int j = 2; j <= 10; j++)
{
if (i % j == 0)
{
if (!divisorFound)
{
cout << " is divisible by ";
}
cout << j << " ";
divisorFound = true;
}
}
if (!divisorFound)
{
cout << " isn't divisible by any numbers";
}
cout << endl;
}
}
return 0;
}

How do I properly run if statements and do while loops with functions?

I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.
I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";
cout << "Pick which program you would like to run." << endl;
cin >> choice;
}
else if (choice == 1){ // -----------Perfect Scores------------
do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
if(a[1]==100)
i++;
return i;
}
{
int score[10];
for(int i=0; i<10; i++)
{
cout << "Enter score " << i+1 << endl;
cin >> score[i];
while(score[i]<0 || score[i] > 100)
{
cout << "Enter score between 1 and 100." << endl;
cin >> score[i];
}
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ------------Larger Than n--------------
else if (choice == 2){
do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
if(A[i]>n)
{
cout << A[i] << endl;
}
}
}
int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------------Roman Numeral Converter------------------
else if (choice == 3){
char repeat;
do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX", "X", "XI", "XII",
"XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit." << endl;
cin >> n;
if(n==0)
exit(0);
do
{
cout << "Enter number between 1 and 20" << endl;
cout << "Enter number or enter 0 to quit" << endl;
cin >> n;
} while(n < 0 || n > 20);
{
cout << "Enter decimal equivalent roman number:" << endl;
cout << "Enter a number between 1 and 20:" << endl;
cin >> n;
if(n==0)
exit(0);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ---------------Monkey Business--------------------
else if(choice == 4){
do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);
{
const int MONKEYS = 3;
double totalFood, averageFood, leastFood, greatestFood;
int foodInfo[MONKEYS][DAYS];
for(int i= 0; i< MONKEYS; i++)
{
cout << "Enter the food information of the monkey" <<
(i + 1) << ":" << endl;
for(int j = 0; j < DAYS; j++)
{
cout << "Day" << (j + 1) << ":" << endl;
cin >> foodInfo[i][j];
while(foodInfo[i][j] < 0)
{
cout << "Day " << (j+1) << ":" << endl;
cin >> foodInfo[i][j];
}
}
cout << endl;
}
totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
averageFood = totalFood / DAYS;
cout << "The average amount of food per day for three monkeys(in pounds):"
<< averageFood << endl;
cout << "The least amount of food per week for monkeys(in pounds) is:"
<< leastFood << endl;
cout << "The greatest amount of food per wek for a monkey is(in pounds):"
<< greatestFood << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
double total = 0;
for(int i = 0; i < mnks; i++)
{
for(int j = 0; j < DAYS; j++)
{
total += food[i][j];
}
}
return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
double least = leastAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal += food[i][j];
}
if(least > weekTotal)
least = weekTotal;
}
return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
double greatest = greatestAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal +=food[i][j];
}
if(greatest < weekTotal)
greatest = weekTotal;
}
return greatest;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------Lottery--------------------
else if (choice == 5){
char repeat;
do{
srand(time(NULL));
int winningDigits[5];
int player[5];
int num;
int matchCount = 0;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 10;
}
cout << "Enter 5 integers in the range of 0 to 9." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
while (num < 0 || num > 9)
{
cout << "Invalid number! It should be in the range of 0 through 9." << endl;
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
}
player[i] = num;
}
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
{
matchCount++;
}
}
cout << "Winning digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << winningDigits[i] << " " << endl;
}
cout << "Player's digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << player[i] << " " << endl;
}
cout << endl << endl << "Number of digits matched: "
<< matchCount << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}
I'm expecting to be able to choose a program to run, repeat it, and repeat the entire program from main menu.
I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:
int choose() { return 0; }
void func1() {}
void func2() {}
int main() {
int choice = 0;
while ( choice = choose() ) {
switch(choice) {
case 1 : func1(); break;
case 2 : func2(); break;
}
}
}
Your main function does not have to be more complex than that.
Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.
Some problems in your code (partly stolen from comments):
you cannot have more than one main
you cannot define functions inside functions
you cannot start an if statement with else if
int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
see here why using namespace std is considered bad practice
if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)

Displaying An Array big elements

I want to display array using a method, if the array has under 200 elements it display all the elements, which works fine for me. The problem is if the array has over 200 elements i want to display the first 100 elements and the last 100 elements of an array. It works if I use an array of 500 elements or even 10000, but I type something like 9999 or 8999 I get long negative integer numbers on the bottom half of my display list but the top half half works. Any advice?
int main()
{
string fileName,text, size;
fstream inText;
int lengthOf = 0;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
inText.open(fileName.c_str() , fstream::in);
if(!inText)
{
cout << "Could Not Open " << fileName << " File" << endl;
exit(EXIT_FAILURE);
}
else
{
inText >> lengthOf;
int * myArray = new int[lengthOf];
for(int i = 0; i < lengthOf; i++)
{
inText >> myArray[i];
}
cout << "Data File Array " << endl;
displayArray(myArray,lengthOf);
}
return 0;
}
void displayArray (int a[], int s)
{
if(s <= 200)
{
for (int i = 0; i < s; ++i)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
else
{
for(int i = 0; i < 100; i++)
{
if(i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
for (int i = s-100; i < s; ++i)
{
if (i%10 == 0)
{
cout << endl;
}
cout << setw(6) << a[i] << " ";
}
cout << endl;
}
}
Printing the array is straight forward, example:
int main()
{
int a[551]; //some random number
int s = 551;
for (int i = 0; i < s; ++i) a[i] = i;
for (int i = 0; i < s; ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << a[i] << " ";
}
return 0;
}
When reading the file you can use std::vector to store the integers, this way you don't have to know how big the array should be before hand. The example below reads text and then tries to convert to integer, this way you know if there was an error in input file.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
std::string fileName;
cout << "Please Enter An Input File Name: ";
getline(cin, fileName);
std::ifstream inText(fileName);
std::vector<int> vec;
std::string temp;
while (inText >> temp)
{
try {
int i = std::stoi(temp);
vec.push_back(i);
}
catch (...) {
cout << temp << " - error reading integer\n";
}
}
for (size_t i = 0; i < vec.size(); ++i)
{
if (i % 10 == 0) cout << "\n";
if (i % 100 == 0) cout << "\n";
cout << std::setw(6) << vec[i] << " ";
}
return 0;
}
Are You sure the problem is with this method? The code seems to work, I can't reproduce Your problem. Maybe You could paste Your whole code (if its not too big)?

matrix from a file input with commas in the file

so my problem is that i have to read from a file that looks like this
input_M1
1,2
9,5
4,1
input_M2
3,2,6,1
4,1,7,8
of course I have to omit the "input_1" and "input_1", and i was able to do that and separate each matrix by itself in a string like this :
1,2
9,5
4,1
and this :
3,2,6.1
4,1,7,8
I was trying to make a dynamic array, i got the rows, with this
while(getline(ss,str1)){
row++;
}
and it prints the number of rows. However, when I do that for the columns :
while(getline(ss,str1,',')){
colmn++;
}
and when I print it out nothing appears.
and here is my whole code :
ifstream inFile;
inFile.open("c:\\Games\\crap.txt");
if (inFile.is_open()){
cout << "File successfully opened" << endl;
}
else{
cout << "Error opening file" << endl;
}
string sMain,sCutOut,firstMatrix,secondMatrix;
int counter = 1;
while(getline(inFile,sMain)){
sCutOut+=(sMain+'\n');
}
//cout << sCutOut << endl;
sCutOut = sCutOut.substr( sCutOut.find("1")+1,sCutOut.length() );
//cout << sCutOut << endl;
firstMatrix = sCutOut.substr( 0,sCutOut.find("input_M2") );
//cout << firstMatrix << endl;
secondMatrix = sCutOut.substr( sCutOut.find("_")+3,sCutOut.length() );
//cout << secondMatrix << endl;
istringstream ss (firstMatrix);
istringstream sn (secondMatrix);
string str1,str2,str3;
int row=0,colmn=0;
while(getline(ss,str1,'\n')){
//cout << str1 << '\n';
row++;
//cout << row << " ";
}
while(getline(ss,str2,',')){
cout << str2 << '\n';
colmn++;
cout << colmn << " ";
}
also, i get this when I try to print out the (firstMatrix) with out the new line I get this :
1,29,54,1
I used C++11 Regular expression library and I applied a simple regular expression to strip off the input_M? lines.
// under g++ 4.8.1 compile:
// $ g++ apply_regex.cpp -std=c++11 -lboost_regex -o apply_regex
#include <fstream>
#include <iostream>
// #include <regex> not implemented yet in g++ 4.8.1 (c++11 standard)
#include <boost/regex.hpp> // using boost libraries
using namespace std;
using boost::regex;
using boost::regex_replace;
int main() {
ifstream inFile;
inFile.open("crap.txt");
if (inFile.is_open()) {
cout << "File successfully opened" << endl;
}
else {
cout << "Error opening file" << endl;
}
string sMain, sCutOut;
while(getline(inFile,sMain)) {
sCutOut+=(sMain+'\n');
}
inFile.close();
cout << "Read lines from crap.txt" << endl;
cout << sCutOut;
// regular expression checks from input_M1 or input_M2 etc..
regex txt_regex("input_M[1-9]+");
string result = regex_replace(sCutOut, txt_regex, "");
cout << "After applying regular expression \"input_M[1-9]+\":" << endl;
cout << result;
return 0;
}
The result of the above code is:
$ ./apply_regex
File successfully opened
Read lines from crap.txt
input_M1
1,2
9,5
4,1
input_M2
3,2,6,1
4,1,7,8
After applying regular expression "input_M[1-9]+":
1,2
9,5
4,1
3,2,6,1
4,1,7,8
Update : i made the matrix into one line and then made them delimited with commas and that is it !
#include<iostream>
#include<fstream>
#include<string>
#include<string.h>
#include<sstream>
#include<vector>
#include<string>
#include<stdlib.h> using namespace std;
int main() {
cout << "Hello! today i'm gonna help you to multiply 2 matrices." << endl;
cout << "But first, we need to discuss the format of the file" << endl;
cout << "help me with the format so i can help you to multiply !" << endl;
cout << "this is how i like the format of the file to be : " << endl;
cout << "--------------------------------" << endl;
cout << "input_M1" << endl;
cout << "number,number," << endl;
cout << "number,number" << endl;
cout << "input_M2" << endl;
cout << "number,number," << endl;
cout << "number,number," << endl;
cout << "--------------------------------" << endl;
cout << "make sure that you put the comma at the end of each line to indicate the end of the line of the matrices [not the input_M1 or input_M2], Have fun !" << endl << endl;
string fileDirct = ""; string fileName = "";
cout << "Now, Enter the file directory, with 2 backslashs, i.e : 'C:\\\\program files'" << endl; cin >> fileDirct; cout << "and Enter the file name, followed by the file type, i.e : 'testData.txt'" << endl; cin >> fileName;
string file = fileDirct+"\\"+fileName; fileDirct = fileDirct.c_str(); ifstream infile;
infile.open(file.c_str());
//ifstream infile( "c:\\Games\\crap.txt");
string sLiner,sStoring,firstMatrix,secondMatrix,sTemp1,sTemp2;
while(infile){
getline( infile, sLiner );
sStoring+=(sLiner+'\n');
}
//cout << sStoring;
sStoring = sStoring.substr( sStoring.find("_")+3,sStoring.length() );
//cout << sStoring;
firstMatrix = sStoring.substr(0,sStoring.find("i"));
//cout << firstMatrix;
secondMatrix = sStoring.substr(sStoring.find("_")+3,sStoring.length());
//cout << secondMatrix;
istringstream ssMtrx1(firstMatrix);
istringstream ssMtrx2(secondMatrix);
istringstream ss(firstMatrix);
istringstream ssk(secondMatrix);
ssMtrx1 >> sTemp1;
istringstream ssR_C1(sTemp1);
int colmn=0;
while(getline(ssR_C1,sTemp2,',')){
colmn++;
}
//cout << colmn;
if (colmn > 6 ){
cout << "the matrix has more than 6 columns";
}
sTemp1.clear();
int row=0;
while(getline(ssMtrx1,sTemp1)){
row++;
}
//cout << row;
if (row > 6 ){
cout << "the matrix has more than 6 rows";
}
//--------------------------------------------------------------------------------
sTemp1.clear();
ssMtrx2 >> sTemp1;
istringstream ssR_C2(sTemp1);
int colmn2=0;
while(getline(ssR_C2,sTemp2,',')){
colmn2++;
}
//cout << colmn2;
if (colmn2 > 6 ){
cout << "the matrix has more than 6 columns";
}
sTemp1.clear();
int row2=0;
while(getline(ssMtrx2,sTemp1)){
row2++;
}
row2 = row2-1;
//cout << row2;
if (row2 > 6 ){
cout << "the matrix has more than 6 rows";
}
//=========================================================================
int** Mtrx1Arry = new int*[row];
for(int i = 0; i < row; ++i){
Mtrx1Arry[i] = new int[colmn];
}
int** Mtrx2Arry = new int*[row2];
for(int i = 0; i < row2; ++i){
Mtrx2Arry[i] = new int[colmn2];
}
sTemp1.clear();
sTemp2.clear();
while(getline(ss,sTemp1,'\n')){
sTemp2+=sTemp1;
}
//cout << sTemp2;
istringstream ss1(sTemp2);
sTemp1.clear();
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn;j++){
getline(ss1,sTemp1,',');
Mtrx1Arry[i][j]=atoi(sTemp1.c_str());
}
}
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn;j++){
//cout << Mtrx1Arry[i][j] << " ";
}
}
//-------------------------------------------------------------
sTemp1.clear();
sTemp2.clear();
while(getline(ssk,sTemp1,'\n')){
sTemp2+=sTemp1;
}
//cout << sTemp2;
istringstream ss2(sTemp2);
sTemp1.clear();
for(int i = 0; i < row2;i++){
for(int j = 0; j < colmn2;j++){
getline(ss2,sTemp1,',');
Mtrx2Arry[i][j]=atoi(sTemp1.c_str());
}
}
for(int i = 0; i < row2;i++){
for(int j = 0; j < colmn2;j++){
//cout << Mtrx2Arry[i][j] << " ";;
}
}
//=============================================================================
// mtrx1[x][y] and mtrx2[z][y] : y has to == z // while the values are under 6
if( (colmn <= 6) && (colmn2 <= 6) && (row <= 6) && (row2 <= 6)){
if (colmn==row2){
cout << "the two matrices can be multiplied " << endl;
cout << "the new matrix will be : " << row << " x " <<colmn2 << endl;
int** MtrxRsltArry = new int*[row];
for(int i = 0; i < row; ++i){
MtrxRsltArry[i] = new int[colmn2];
}
for(int i = 0; i < row;i++){
for(int j = 0; j < colmn2;j++){
MtrxRsltArry[i][j]=0;
for(int k=0; k < colmn; k++){
MtrxRsltArry[i][j]=MtrxRsltArry[i][j]+(Mtrx1Arry[i][k]*Mtrx2Arry[k][j]);
}
}
}
for(int i = 0; i < row;i++){
//cout << endl;
for(int j = 0; j < colmn2;j++){
//cout << MtrxRsltArry[i][j] << ",";;
}
}
// cout << endl;
ofstream outFile;
string cat = fileDirct+"\\result.txt";
outFile.open(cat.c_str());
cout << "the result.txt has been created at : " << fileDirct << endl;
outFile << "result_M";
for(int i = 0; i < row;i++){
outFile << endl;
for(int j = 0; j < colmn2;j++){
outFile << MtrxRsltArry[i][j] << ",";
}
}
}
else{
cout << "the two matrices can not be multiplied ";
}
}
for(int i = 0; i < colmn; ++i) {
delete [] Mtrx1Arry[i];
}
delete [] Mtrx1Arry;
for(int i = 0; i < colmn2; ++i) {
delete [] Mtrx2Arry[i];
}
delete [] Mtrx2Arry;
return 0; }

Compare string array with a single string entered by user , it also tell that it is found 2times &at that position

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[3][10], search[10];
int i, t = 0, k;
cout << "Enter 3 Names Of Fruit" << endl;
for (i = 0; i < 3; i++)
cin >> str[i];
cout << "Enter Fruit Name To Search" << endl;
cin >> search;
for (i = 0; i < 3; i++)
{
if (search == str[i]) // if statement is not giving true
{
t++;
k = i;
}
}
cout << "The " << search << " is found " << t <<
" times and is at position " << k << endl;
getch();
}
You have to use std::strcmp from cstring:
Full code:
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;
using std::cin;
using std::strcmp;
int main() {
char str[3][10], search[10];
int i, t = 0, k;
cout<<"Enter 3 Names Of Fruit"<<endl;
for( i = 0; i < 3; i++ )
cin>>str[i];
cout<<"Enter Fruit Name To Search"<<endl;
cin>>search;
for( i = 0; i < 3; i++ ) {
if( strcmp(search, str[i]) == 0)
{
t++;
k=i;
}
}
cout << "The " << search << " is found " << t
<< " times and is at position " << k + 1 << endl;
cin.get();
return 0;
}
Sample run:
Enter 3 Names Of Fruit
banana mango orange
Enter Fruit Name To Search
mango
The mango is found 1 times and is at position 2