i = test->info->returnCustomerID();
Did you ever declare 'i' ?
i = test->info->returnCustomerID();
Did you ever declare 'i' ?
A good way to go about it sometimes is to only consider the first error line. And then if you can't solve that look at the next line and see how that error might relate to the previous one.
You need to understand the error messages; if you do not like the format of the errors, try compiling on a different compiler to get a different looking error.
error C2664: 'strlen' : cannot convert parameter 1 from 'std::string *' to 'const char *'
For example, an error like this one, you're trying to pass a std:string as a parameter to a function that accepts a const char* as a parameter.
They need to be public.
for ( int b=x;b>=0;b--)
{
for (int z=1;z<=b;z++)
{
cout<<"a";
}
<<endl
cout
}
// and this is for the pyramid
for (int c=1;c<=height*2-1;c=c+2)
{
for(int d=1;d<=c;d++)
{
cout<<"x";
}
cout<<endl;
}
Is the "a" what is meant to be your blank spaces? If so I think you're looking at it the wrong way.
The first line will have (height - 1) blank spaces, because it will be half of the bottom row (excluding the middle X), then decreases by 1 for each new row.
THIS IS UNTESTED BUT SHOULD WORK
for (int level=1;level<=height; level--)
{
for(int blanks= height-1; blanks > 0; blanks--)
{
cout << " ";
}
for(int d=0;d < (level*2 -1); d++)
{
cout<<"x";
}
cout << endl;
}
get input height
nunmberOfBlanks = height - 1
numberOfX = 1
level = 1
for 1 to height
for 1 to numberOfBlanks
print " "
endfor
for 1 to numberOfX
print "X"
endfor
numberOfBlanks = numberOfBlanks - 1
level++
numberOfX = 2 * level
endfor
I think that would do it
A pyramid such as that grows at a rate of (2x - 1), or one less than 2 times x.
***X 2*1 = 2 -1 = 1
**XXX 2*2 = 4 - 1 = 3
*XXXXX 2*3 = 6 - 1 = 5
XXXXXXX 2*4 = 8 - 1 = 7
A possible way to get what you're looking for is to output a blank space " " in front of each row.
And I'm guessing from looking the asterisks that you're going to want to start at one less than the height (so in the previous example, 3) then decrement it until you're left with 0 and output the last line without any spaces.
And since it is an assignment, I would recommend changing your variable names to something meaningful, (ie height, blankSpaces, etc.)
Visual C++ can also automatically indent, and correct code that's gotten out of whack. Select the code, use ctrl-k f. I don't care for the exact way it handles switches, but it does the bulk of the work fine.
I have a tendency to work away from Visual C++, no idea why. I think its not aesthetically pleasing enough for me and I find it a bit cumbersome.
I usually work with Notepad++ for small pieces of code, Eclipse Ganymede for usual everyday projects.
I know what you mean about the switch statements, it seems to be universal in the way it gets indented.
switch(num)
{
case 1:
{
}
case 2:
{
}
default:
{
}
}
so ugly.
#include <iostream>
using namespace std;
bool Palindrome(string pal, int index, int length)
{
while(index < length)
{
if (length == 0 || length == 1)
return true;
if (pal[index] != pal[length - index])
return false;
index++;
}
return true;
}
int main () {
string word;
cout << "Enter your prospective palindrome." << endl;
cin >> word;
bool result = Palindrome(word, 0, word.length()-1);
cout << word << "is";
if(!result)
cout << "NOT";
cout << " a Palindrome." << endl;
return 0;
}
And for the record, I love the "Correct indentation" function of both Notepad++ and Eclipse Ganymede <3<3<3
Don't ever ever try to get cute with your code. Professors really seriously hate that.
Heres some great links;
http://www.wikihow.com/Find-Motivation-to-do-Homework
http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node42.html
And a possible followup link:
http://www.mcdonalds.com/corp/career.html
Check your braces. There are problems with them.
That is fairly obvious.
Well here's the deal. I got really interested in programming around 12 years old or so. I was unable to teach myself, but I still had a bunch of books. I get older, join the military, serve 6 years, and here I am finally 26 and just learning it for real.
I have just a few old books which I previously thought would be worthless due to changes that have made it so. But on the other hand, I kind of feel like a lot is being left out by the way that they are presenting the language now. So I went ahead and started poking around at the boxes in my attic and pulled out the following:
C++ In Plain English - Brian Overland 1996
Rescued By C++ Second Edition - Kris Jamsa 1996
Teach Yourself Visual C++ 4 in 21 Days 1996
Visual C++ Tutorials, Development System for Windows 95 and Windows NT 1995
Pretty neat thing was the multicolored markers I used to write my name in a couple of them ;D
I read a post earlier about C++ books and whatnot.
I have a few books from my yesteryears (10-13 years ago) on C++ mostly. How useful would these books be in terms of the language as it has progressed in the last 10 or so years. Are they even worth digging up or not.
Looking for an opinion, thanks.
I have a 10+ year old Accelerated C++ book covered in oil in the trunk of my car. Want it? :D
Running this code
//============================================================================
// Name : mashley09_daniweb.cpp
// Author : chococrack
// Version : 1.0
//============================================================================
1.) I have to enter #1 choice twice before it displays what number needing to translate.
2.) when i enter choice 2 it does not display the roman numerals for 1 through 10
The mingw compiler gives me the following error (in Eclipse)
**** Rebuild of configuration Debug for project mashley09_daniweb ******** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\mashley09_daniweb.o ..\src\mashley09_daniweb.cpp
..\src\mashley09_daniweb.cpp:2: error: expected unqualified-id before numeric constant
..\src\mashley09_daniweb.cpp:2: error: invalid token
Build error occurred, build is stopped
Time consumed: 78 ms.
Well it's dead obvious... theres a SMILEY FACE IN YOUR CODE! o.0
cin >> name >> lastname;
would work just fine too
int 2dArray[4][5];
int x = 1;
for(int i=0; i<4; i++)
{
for(int j=0; i<5; i++)
{
2dArray[i][j] = x;
x++;
}
}
this sets up an array of 20 integers, 1-20 as follows
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
So to average the values, you'd want to do something like this:
int rowAvg = 0, colAvg = 0;
double finalRowAvg=0;
double finalColAvg=0;
for(int m=0; m<4; m++)
{
for(int n=0; n<5; n++)
{
rowAvg += 2dArray[m][n];
}
finalRowAvg = rowAvg / 4;
cout << "Row Avg for Row: " << m << " is " << finalRowAvg;
rowAvg = 0; // reset row average for next row
finalRowAvg = 0.0; // reset final average for next row
}
Then just do the exact opposite to get the columns:
for(int a=0; a<5; a++)
{
for(int b=0; b<4; b++)
{
colAvg += 2dArray[b][a];
}
finalColAvg = colAvg/a;
cout << "Column Avg for Col: " << a << " is " << finalColAvg ;
colAvg = 0; // reset
finalColAvg = 0.0; // reset
}
I know this doesn't solve your problem, but I hope it's helped to clarify what you're trying to accomplish (which I'm still trying to figure out :( )
ifstream current_file;
current_file.open(file.c_str());
// do stuff
current_file.close();
Does that work?
//function prototypes
void rev_str();
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int,int);
// main
int main()
{
. . .
return 0;
}
// functions
. . .
Side note: You only need to seed the random number generator once to use it for the application.
// . . .
int main()
{
srand((unsigned)time(0));
// . . .
}
// . . .
If you leave it as is, it will only perform 10 repetitions of the loop, including invalid inputs. Then you'd only get the first 10 - (number of errors) even integers in your summation. My suggestion is this:
for (int ctr=1; ctr<=10; )
{
cout<<"Enter a number"<<endl;
cin>>num;
if (num%two==zero)
{
sum=sum+num;
ctr++; // only increment when a valid summation occurs
}
else
cout<<"Please enter an even number"<<endl;
cout<<"Enter a number"<<endl;
cin>>num;
}
Let me explain the for structure I used here.
for (int ctr=1; ctr<=10; )
- your original for loop incremented ctr on each run (int ctr=1; ctr<=10; ctr++)
- what I'm doing different is removing the incrementing of ctr from every run, so that
invalid numbers that are input (odds) do not count toward the number of repetitions
- this way you get 10 total numbers and not a lower number
Wheres the closing brace for your main function?
for (i=1;i<=blankCount; i++){
cout <<" \t";
}
for(i=1; i <= totalDates; i++){
cout << i << "\t";
dayCount++;
if (dayCount==7){
cout << "\n";
dayCount=0;}
}
Ok... so for instance April... the output looks like this:
I will display the calendar for the month you choose in 2008.
please input the month you wish to see corisponding with its number.
4
April2008
Sun Mon Tues Wed Thurs Fri Sat
=== === === === === === ===
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Do you wish to see another month? Y/N or y/n?
Here's what your code is doing:
for (i=1;i<=blankCount; i++){ // blankCount for April
cout <<" \t"; // is 2, print 2 blanks
}
for(i=1; i <= totalDates; i++){ // from 1 to 7, dayCount
cout << i << "\t"; // incremented, but does
dayCount++; // not take into account
if (dayCount==7){ // value of blankCount
cout << "\n";
dayCount=0;}
}
You need to give the value of blankCount to dayCount+1 before getting to this section in your code, then, the dayCount will begin at 2, rather than 0
SOLUTION:
TWO files: myClass.h main.cpp
//
// myClass.h
#ifndef MYCLASS_H_
#define MYCLASS_H_
template <class element_type>
class myClass
{
public:
myClass();
~myClass();
};
template <class element_type>
myClass<element_type>::myClass()
{
}
template <class element_type>
myClass<element_type>::~myClass()
{
}
#endif /* MYCLASS_H_ */
//
// main.cpp
#include "myClass.h"
int main()
{
myClass<int> classOBJ;
return 0;
}
That's got to be it, because I do know that if I stack them all together in one file, everything runs without a problem.
The only compiler I know of that actually allows this separation so far (in my studies) is the Sun Studio 12 "CC" compiler
That link is golden.
try including iostream too.
No change.
I have three files (myClass.h, myClass.cpp, and main.cpp)
//
// myClass.h
#ifndef MYCLASS_H_
#define MYCLASS_H_
template <class element_type>
class myClass
{
public:
myClass();
~myClass();
};
#endif /* MYCLASS_H_ */
//
// myClass.cpp
#include "myClass.h"
template <class element_type>
myClass<element_type>::myClass()
{
}
template <class element_type>
myClass<element_type>::~myClass()
{
}
//
// main.cpp
#include "myClass.h"
int main()
{
myClass<int> classOBJ;
return 0;
}
I tried compiling this in VC++ Express, Dev-Cpp, and Eclipse
All give me roughly the same error. From eclipse
undefined reference to `myClass<int>::~myClass()' main.cpp heapProj 15 C/C++ Problem
undefined reference to `myClass<int>::myClass()' main.cpp heapProj 12 C/C++ Problem
This leads me to believe that I'm coding something wrong, but I just can't track it down for some reason. What's the problem?
int score;
char grade;
// read in total score
cout << endl;
cout << "Enter total score (float, must be <= 100): ";
cin >> score;
if (score >= 85)
grade = 'A';
else if (score >= 75)
grade = 'B';
else if (score >= 65)
grade = 'C';
else if (score >= 55)
grade = 'D';
else
grade = 'F';
// display the result
cout << endl;
cout << "Your grade for CMSC 101 is: " << grade << endl;
return (0); // terminate with success
It compiles and runs, you just have to supply the right input file.
inFile.open("C:\\hw_5-1_ext1.txt");
C:\hw_5-1_ext1.txt must exist and be in the correct format.
I ran this code with a file I created that looked like:
123456 100 77 88 55.1
654321 88 77 66 55
I saved the file to C:\hw_5-1_ext1.txt and compiled the program and it ran fine.
Student GPA Special Note
123456 80.025 Honor Roll
654321 71.500 Honor RollCourse 1 average: 94.000
Course 2 average: 77.000
Course 3 average: 77.000
Course 4 average: 55.050
Meh.. are you sure your math is right in there? o.O Are you trying to find the GPA? Because it totaly not doing that. You're getting the average of the grades, but that's about it.
What is the question... your code compiles and does everything it's supposed to?
delete Operator (C++):
http://msdn.microsoft.com/en-us/library/h6227113(VS.80).aspx
Dynamic Memory:
http://www.cplusplus.com/doc/tutorial/dynamic.html
wrap it in braces
{
blah
}
Also, in your if statement, I think you're missing an else?
if ( ( i == 2 ) or ( i % 2 != 0 ) )
{
p = i;
}
else // ????
{
for ( int j = 2; j > p; j++ )
{
if ( p % j != 0 )
t = p;
}
cout << t << endl;
}
Variables, pointers to variables, pointers to pointers, pointers to objects, . . .
I mean, yes... any variable
int, string, char, double, etc etc etc
Trust me, all of our first attempts were without doubt riddled with errors.
Check out a visual: (I love drawrings) hehe
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Function declarations
char menu();
void circle();
void rectangle();
void triangle();
// Main function
int main()
{
//Declaration of Variables.
char choice;
bool loop = true;
while( loop )
{
choice = menu();
switch (choice)
{
case 'c':
case 'C': // Circle
circle();
break;
case 'r':
case 'R':
rectangle();
break;
case 't':
case 'T':
triangle();
break;
case 'q':
case 'Q':
loop = false;
break;
}
}
return 0;
}
//************************
//FUNCTIONS *
//************************
char menu()
{
char temp;
cout << "\tGeometry Calculator" << endl << endl;
cout << setw(30) << "Calculate the Area of a [C]ircle" << endl;
cout << setw(30) << "Calculate the Area of a [R]ectangle" << endl;
cout << setw(30) << " Calculate the Area of a [T]riangle" << endl;
cout << "[Q]uit" << endl;
cout << setw(10) << "Enter your choice:";
cin >> temp;
return temp;
}
void circle()
{
double circleArea, radius;
const double PI = 3.14159;
cout << "Enter the radius of the circle: ";
cin >> radius;
if (radius < 0)
{
cout << "Number must be greater than 0. Try again" << endl;
}
else
{
circleArea = PI * pow(radius, 2);
cout << "The area of the circle is: " << circleArea;
}
cout << endl;
}
void rectangle()
{ // THIS BRACE WAS AN OPEN PARENTHESIS!!!!!!!!!!
}
void triangle()
{
}
Fill in the blanks ;)
ALSO ONE LAST THING:
When you do this:
//Declaration …
Oh my wow.
You are redeclaring every variable you already declared in main. You are re-doing a switch for choice unecessarily in every function, you do not need to do all these things. Let me give you some direction.. Give it about 4-5 mins I'll post some code.
void circle()
{
double circleArea, rectangleArea, triangleArea,
length, width, height, base, radius;
char choice;
const double PI = 3.14159;
switch (choice)
{
case 'C':
case 'c':
case '1':
You're making a switch and not giving "choice" a value. It's just getting garbage.
I'm still reading through it, I'll edit this post if I find more problems.
char menu()
{
char temp;
cout << "\tGeometry Calculator" << endl << endl;
cout << setw(30) << "1. Calculate the Area of a [C]ircle" << endl;
cout << setw(30) << "2. Calculate the Area of a [R]ectangle" << endl;
cout << setw(30) << "3. Calculate the Area of a [T]riangle" << endl;
cout << "4. [Q]uit" << endl;
cout << setw(10) << "Enter your choice (1-4)\n";
cin >> temp;
return temp;
}
If you're basing your selections on a char, why are you asking for a number (1-4)? And someone correct me if I'm wrong but doesn't an entry of a number into a char return the ascii-equivalent?
Still reading...
Does moving your function prototypes help?
// Hw-4_Bhasin.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
void rev_str(void);
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int, int);
using namespace std;
int main()
{
char option;
cout<<"\n Please choose from the given menu.";
cout<<"\n R{Reverse String] M[Matrix] Q[Quit]."<<endl;
cin>> option;
switch(option)
{
case 'R':
rev_str();
break;
case 'r':
rev_str();
break;
}
system("pause");
return 0;
}
void rev_str(void)
{
const int MAX = 100;
char Input_String[MAX];
cout<<"Please enter a string."<<endl;
cin.get(Input_String, MAX);
cout<<Input_String;
for(int i=0; i<MAX; i--)
cout<<Input_String<<endl;
system("pause");
return;
}
Here's a silly little illustration I drew just now:
Yikes. Also, when you're doing the checks for the P, S, and R,
if (one=='p' || one=='P' && two=='p' || two == 'P')
if one is 'p', its going to be true and skip the rest of the condition
a workaround to this is to check for uppercase inputs and convert them to lowercase, then use those in the comparisions
if(one=='P')
one = 'p'
...
if(one=='p' && two='p')
cout << "tie . . .";
...
# include <iostream>
using namespace std;
int main()
{
char one, two, answer;
do {
system("cls");
cout<<"\n\n\t\tSelect a key from the following choices:";
cout<<"\n\n\t\t\tKey:\t\tMeaning:";
cout<<"\n\n\t\t\t P\t\t paper";
cout<<"\n\t\t\t R\t\t rock";
cout<<"\n\t\t\t S\t\t scissor";
cout<<"\n\nEnter player 1:";
cin>>one;
cin.ignore();
if(one=='P')
one='p';
if(one=='R')
one='r';
if(one=='S')
one='s';
cout<<"Enter player 2:";
cin>>two;
cin.ignore();
if(two=='P')
two='p';
if(two=='R')
two='r';
if(two=='S')
two='s';
if (one=='p' && two=='p')
{
cout<<"\nResult: Tie!\nNobody wins!";
}
else if (one=='r' && two=='r')
{
cout<<"\nResult: Tie! \nNobody wins!";
}
else if (one=='s' && two=='s')
{
cout<<"\n Result: Tie! \nNobody wins!";
}
else if (one=='p' && two=='r')
{
cout<<"\n\nResult: Player 1 wins! \n Basis: Paper Covers Rock!";
}
else if (one=='r' && two=='s')
{
cout<<"\nResult: Player 1 wins! \n Basis: Rock Breaks Scissor!";
}
else if (one=='s' && two=='p')
{
cout<<"\nResult: Player 1 wins! \n Basis: Scissor Cuts Paper!";
}
else if (one=='r' && two=='p')
{
cout<<"\n\nResult: Player 2 wins! \n Basis: Paper Covers Rock!";
}
else if (one=='s' && two=='r')
{
cout<<"\nResult: Player 2 wins! \n Basis: Rock Breaks Scissor!";
}
else if …
while (answer=='Y'||'y');
Is like saying
"WHILE ANSWER IS 'Y' OR "
"WHILE 'y'" <-- What's 'y'?
Your while loop isn't terminating. I'm sure the myriad of if statements using this logic are failing too.
Try this instead:
while(answer=='Y' || answer=='y')
ar[x][y]
it was a 2d array wasn't it?
*claps*
You didn't "model" a binary string like he told you to ( string binary="00000000";)
I think what he wants you to do is something similar to what he was doing in the BtoD routine he gave you, where you directly change the string elements using an array-like assignment.
binary[position] = '1';
or
binary[position] = '0';
But everything does appear as though it will work fine. Those are just really suggestions to make it follow as close to the instructions as possible. Good job getting through it though!
So what are you waiting for?
I think whats happening is that on the last runthrough of your while loop, it is reading the last line, but not an EOF
Then its running through it again, getting squat for info?
Also dfile.good() only is checking for no errors, not end of file.
while(dfile)
{
...
}
That also seems more efficient than what I had in mind, which was running a task that would check systemdate for saturday and execute "whatever" once on that day.
Here a link that gives a code example to:
"create a task that is scheduled to execute Notepad on a weekly basis. The task contains a weekly trigger that specifies a start boundary, a weeks interval, and a day of the week for the task to start on. The task also contains an action that specifies the task to execute Notepad."
Also this looks more attractive, if you're creating this for a windows environment:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
A sample for displaying date and time using SYSTEMTIME is as follows. This program displays the current Coordinated Universal date and Time, using GetSystemTime function.
#include <Windows.h>
#include <stdio.h>
void main()
{
SYSTEMTIME st;
GetSystemTime(&st);
printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
}