Facing error in c++ code [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am writing code of ACM problem in which we have to check possibilities of different items. It's some minor error in the code.
#include<iostream>
using namespace std;
void CheckPossibilities( int numItems, int maxWeights )
{
if( numItems <= 0 )
{
cout << "Invalid Items";
}
if ( maxWeights <= 0 )
{
cout << "Impossible";
}
while( maxWeights > 0 )
{
if(numItems%2==0) //for even
{
numItems = numItems / 2;
maxWeights--;
}
else
{
numItems = (numItems -1)/ 2; //for odd
maxWeights--;
}
}
if( numItems <= 1 )
{
cout << "Possible";
}
else
{
cout << "Impossible";
}
}
void main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1 "AND" maxWeights1<<endl;
cout<<CheckPossibilities(numItems1, maxWeights1);
}

Your mistakes were trying to cout multiple strings in one line without concatting them in some way, either seperate with a << or a +. You also cant cout a void function because it tries to output void, you just need to call it and let the function do the outputting. With errors fixed the main should be
int main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1+"AND"+maxWeights1<<endl;
CheckPossibilities(numItems1, maxWeights1);
return 0;
}
Next time look at what line the error is thrown on when compiling and search those specific error because these were really simple and specific syntax errors that could be found by a google search easily.

void main()
{
int numItems1, maxWeights1;
cout << "enter numItems" << endl;
cin >> numItems1;
cout << "maxWeights" << endl;
cin >> maxWeights1;
cout << numItems1 << "AND" << maxWeights1 << endl;
CheckPossibilities(numItems1, maxWeights1);
}
you can never do this: cout<<CheckPossibilities(numItems1, maxWeights1); cout take standard output stream, not functions. And also you forgot to put << in cout << numItems1 << "AND" << maxWeights1 << endl; in this form, your code build succesful.

Related

I got a memory error and i dont know whats cauising it in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Hello this code is part of a big code and it causing a lot of problem i dont know why. i tried to use structer and vectors its ggot an error about vector out of range then i switched to class but still got same error im now compiling just problematic and its looks like a memory error. how can i fix this ?
#include <time.h>
#include <locale.h>
#include <vector>
#include <exception>
using namespace std;
class işlem {
string işlemdetay; int işlemtutar; time_t işlemtarih; int hesapno;
public:
vector <işlem> işlemler;
işlem() { işlemdetay = " "; işlemtutar = 0; işlemtarih = time_t(0); hesapno = 0; };
işlem(string İşlemdetay, int İşlemtutar, int Hesapno) {
işlemdetay = İşlemdetay; işlemtutar = İşlemtutar; hesapno = Hesapno;
işlem newişlem(işlemdetay, işlemtutar, hesapno);
işlemler.push_back(newişlem);
}
void listele(int hesapid) {
int size = işlemler.size();
for (; size >= 0; size--)
{
if (işlemler[size].hesapno == hesapno)
{
//cout << "İşlem Tarihi: " << işlemler[size].işlemtarih << endl;
cout << "İşlem Tutar: " << işlemler[size].işlemtutar << endl;
cout << "İşlem Detayı: " << işlemler[size].işlemdetay << endl;
}
}
}
};
int main()
{
try
{
işlem newişlem("Hesap oluşturuldu.", 400, 1);
}
catch (const std::exception e)
{
cout << e.what();
}
}```
işlemler[işlemler.size()] is out-of-range. The initial value of size should be işlemler.size() - 1, not işlemler.size().

illegal else with no matching if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
It seems that my if statements are not working and I'm getting the "illegal else without matching if" error message. any help would be great, thanks.
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int igame = 0;
int main()
{
Sleep(1000);
cout << "welcome to the Wild Casino!";
Sleep(1000);
cout << "\nplease select a game to play. 1 for Slots, 2 for Roulette, and 3 for Blackjack: ";
cin >> igame;
if (igame == 1);
{
cout << "\nWelcome to Slots";
}
else if (igame == 2);
{
cout << "\nWelcome to Roulette";
}
else
{
cout << "\nWelcome to Blackjack";
}
Sleep(1000000);
return 0;
}
if (igame == 1);
You have an extra semicolon at the end - this is equivalent to
if (igame == 1) { }
Your code creates an ill-formed program:
if (igame == 1) { }
{ // block not attached to if
cout << "\nWelcome to Slots";
}
else if (igame == 2) { } // this else has no matching if
{
cout << "\nWelcome to Roulette";
}
else // this else has no matching if
{
cout << "\nWelcome to Blackjack";
}

trying to solve project euler number 14 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I´m trying to solve project euler problem number 14 and i have the code almost ready, but it keeps me giving the wrong answer.. Why it doesn't count more steps?? Thanks, sorry for the lack of commentary..
#include <iostream>
int collatz_length(int number);
int main() {
using namespace std;
int size_sequence, max_sequence = 0, number_ = 1000000, num;
while (number_>1) {
size_sequence = collatz_length(number_);
if (size_sequence > max_sequence) {
max_sequence = size_sequence;
num = number_;
cout << "size " << size_sequence
<< " starting number " << num << endl;
}
number_--;
}
cout << "The longest sequence has "
<< max_sequence << " steps, starting from the number: " << num << endl;
cin.get();
return 0;
}
int collatz_length(int number) {
using namespace std;
int size_sequence = 0;
while (number > 1) {
if ((number % 2) == 0){
number /= 2;
}
else {
number = (3 * number + 1);
}
size_sequence++;
}
return size_sequence;
}
It is possible that 3*n+1 will overflow an int. Perhaps you should use a uint64_t?

Creating a Tic Tac Toe with 4x4 grid, cout and endl unidentified? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is the code, i am a beginner at coding and do not know how to find out the errors after following various different tutorials. I keep coming up with errors as 'cout' and 'endl' are undeclared identifiers.
#include <iostream>
#define ORDER 4
void PrintGrid(char [ORDER][ORDER]);
bool CheckGrid( char [ORDER][ORDER]);
int main(void){
char grid[ORDER][ORDER] ;
int j,k,l;
for(j=0;j<ORDER;j++)
for(k=0;k<ORDER;k++)
grid[j][k]=' ';
grid[2][3]='X';
PrintGrid(grid);
return 0;
}
void PrintGrid(char g[ORDER][ORDER]){
for (int j=0;j<ORDER;j++){
for(int l=0;l<2*ORDER +1;l++)
cout << '-';
cout << endl <<'|';
for (int k=0;k<ORDER;k++)
cout << g[j][k] <<'|';
cout << endl;
}
for(int l=0;l<2*ORDER +1;l++)
cout << '-';
cout << endl;
}
bool CheckGrid( char g[ORDER][ORDER]){
// check horiz
// untested
int k,j;
for( k=0;k<ORDER;k++){
for( j=1;j<ORDER;j++)
if(g[0][k]!=g[k][j]) break;
if(j==ORDER) return true;
}
for( k=0;k<ORDER;k++){
for( j=1;j<ORDER;j++)
if(g[k][0]!=g[k][j]) break;
if(j==ORDER) return true;
}
for( k=0;k<ORDER;k++){
if(g[0][0]!=g[k][k]) break;
if(k==ORDER) return true;
}
for( k=0;k<ORDER;k++){
if(g[0][ORDER-1]!=g[ORDER-k][ORDER-k]) break;
if(k==ORDER) return true;
}
return false;
}
cout and endl are in the std namespace. You have to reference them like this:
std::cout << ... << std::endl;
unless you are using them:
using std::cout;
using std::endl;
You can also include the whole namespace:
using namespace std;

C++ - How to count how many times occur the same string in a list

I searched a lot on google and stackoverflow but I couldn't find my answer. I'm actually reading a C++ book (C++ Primer 5th Edition) and they're asking me to do an exercise.
"Write a program that reads several transactions and counts how many transactions occur for each ISBN" (Console Project)
This is my code atm :
Sales_item currentItem, item;
if (cin >> currentItem)
{
int cnt = 1;
while (cin >> item)
{
if (currentItem.isbn() == item.isbn())
{
++cnt;
}
else
{
cout << currentItem.isbn() << " occurs " << cnt << " times " << endl;
cnt = 1;
currentItem = item;
}
}
cout << item.isbn() << " occurs " << cnt << " times " << endl;
}
I won't explain how work the transactions so I'm gonna ask it in another way.
I type in my console 6(or more) random strings as exemple:
101A
102A
101A
101A
103A
102A
I want the result the result (output) to be:
101A occurs 3 times.
102A occurs 2 times.
103A occurs 1 times.
How would you do that?
Using std::map instead of list will be easier.
int main()
{
map<string,int> stringMap;
for (int i=0;i<3;i++)
{
cout<<"Enter string: ";
string s;
cin>>s;
if(stringMap.find(s)!=stringMap.end())
{
stringMap[s]++;
}
else
{
stringMap[s]=1;
}
}
for (map<string,int>::const_iterator itr = stringMap.cbegin(); itr!=stringMap.cend(); ++itr)
{
if(itr->second > 1)
cout<<itr->first << " occurs "<<itr->second<<" times"<<endl;
else
cout<<itr->first << " occurs "<<itr->second<<" time"<<endl;
}
return 0;
}
There are multiple approaches to a question like this, so the best would depend on your constraints. My approach would be:
while(GetInputString(str)) {
myStruct* ptr = existingList.Find(str);
if (!ptr) {
existingList.Add(str);
} else {
ptr->IncrementCount();
}
}
I've tried not to solve your problem for you - hopefully the answer gives you a template to work with...
#include <iostream>
#include <vector>
#include "Sales_item.h"
using namespace std;
void book_transactions(string isbn, int count);
int main()
{
vector<Sales_item> book_vec;
Sales_item book;
int total_transactions = 1;
// Reads in Sales_item objects
while (cin >> book)
{
book_vec.push_back(book);
}
// Compares ISBNs in vector, if two isbns are equal total transactions increases
for (int i = 0; i < book_vec.size() - 1; i++)
{
if (book_vec[i].isbn() == book_vec[i+1].isbn())
{
total_transactions = book_vec[i].get_units_sold() + book_vec[i+1].get_units_sold();
book_transactions(book_vec[i].isbn(), total_transactions);
}
}
}
void book_transactions(string isbn, int count)
{
cout << "ISBN: " << isbn << " " << "Transactions: " << count << endl;
}
I went into the Sales_item.h class and added a method to get the number of units sold. There was already a class member (unsigned return type) under private. I just created a getter and then created a vector of Sales_items. This solution seems to work. I'm sure the intended problem is not for an N set of sales_items, however this should work as a "duct-tape" solution. There is no need to play around with pointers at this point. A simple vector array and an algorithm to compare adjacent objects works just fine.