twomers 408 Posting Virtuoso

>> That's all?
Is that a question or a statement?

>> I just don't get why to got rid of all their code which just needed an extra line to make it valid.
I don't get why you keep on complaining about things I do, but you do.
Just accept that's how I program and get over it. Not everybody has to conform to your style of programming. That, imthewee, is mine.
Good bye.

>> If I have offended you I am sorry. I didn't mean it.
You frustrated me for sure but to offend me... You'd probably have to compare my coding standards to something dead and ugly.

iamthwee commented: I'm sorry if I have hurt your feelings. +11
twomers 408 Posting Virtuoso

>> feel yourself unequipped to defend it
I did defend it:

The other two code samples use the comparison operator to determine if equality exists between the two parameters and acting on this result they return a value rather than returning the result.

Gah!

>> OP needed was another line of code to make their original statement valid.
Function.
Yes. But the OP's underlying understanding of C++ and scope showed that there were other issues. I may be wrong but I assumed (s)he thought that the bool variable was accessable within the function.

>> I finally understand it
That doesn't sound like (s)he was confused.

Do you really think that was obfuscated? Really?

>> Please explain why having a void inside the main makes it clearer?
Are you really just trying to attack everything I say?

>> It's a secret...
ie - the same way I do it.

twomers 408 Posting Virtuoso

>> Is it.
I believe so anyway.

>> Clarity is paramount over everything else.
I agree, but I don't find the code I presented to be particularily (or in earnest any way), confusing. Do you? If yes, why? It's (very nearly), the same thing as using operators in if statements, control operations in loops etc. If one can handle them I can't see why one should have problems handleing/understanding that.

But all this said - everyone has their own programming style. What I posted earlier is what I tend to do, iamthewee. If you prefer other ways about it then that's your right.

Just looking back at my first post I said "That should simply be:", while what I should have said is "That could simply be:"

>> Please explain.
The other two code samples use the comparison operator to determine if equality exists between the two parameters and acting on this result they return a value rather than returning the result. The other way just returns what the operator returns. I'm not 'all up' on my efficiencies but maybe some compilers will negate any differences (which will be tiny anyway), but I believe the return operator is better pratise.

>> btw you don't need to void inside your int main.
True. I think it makes it clearer which, as you mentioned, is paramount ;)

BTW, how would you accomplish returning bools? Assuming there is processing being done before the …

twomers 408 Posting Virtuoso

>> HI CAN U PLS HELP ME IN ADDING SOMETHING TO THIS PROGRAM TO BE LOOK GOOD.
Code tags! - [code]

[/code]

twomers 408 Posting Virtuoso

Just cause it's easier:

bool eq1( int &v1, int &v2 ) {
  if ( [B][U]v1 == v2[/U][/B] )
    return true;
  else 
    return false;
}
bool eq2( int &v1, int &v2 ) {
  return ([B][U]v1==v2[/U][/B] ? true : false);
}
bool eq3( int &v1, int &v2 ) {
  return [B][U]v1==v2[/U][/B];
}

int main( void ) {
  int num1 = 1, num2 = 2;

  std::cout<< eq1(num1, num2) << "\n";
  std::cout<< eq2(num1, num2) << "\n";
  std::cout<< eq3(num1, num2) << "\n";
  
  return 0;
}

Might also be good for micro optimisations.

Edit: Also what other way is there of doing this which doesn't result in using a comparison operator in the process? Why use more than one when you don't need to? I bolded and underlined examples of such in the above code.

twomers 408 Posting Virtuoso

OK.

You know you can do:

if ( something == something_else ) { ... }

If something does indeed equal something_else then the parameters return true through the == operator, right (and if it doesn't it returns false)? Well, returning something==something_else from a function is the exact same thing.

It just looks confusing is all. Once you think about it it makes sense.

Also, ignore the &'s before the value name. That just means that the values are being passed into the function by reference.


To further the above you can use all kinds of operators for returning bools (and more):

int greater_than( int val1, int val2 ) { // returns true if val1>val2
  return val1>val2;
}

int not_equal_to( ... ) { // same parameters as above
  return val1 != val2;
}

// etc etc etc
twomers 408 Posting Virtuoso

What errors are you getting?

Edit1 to OP: Put in your units :)
Edit2: You might have to cast. I think sqrt takes a double ... Not certain now though.

twomers 408 Posting Virtuoso

Mmmmmmmm. Caffeine. Good idea, vegaseat.

twomers 408 Posting Virtuoso

Have you #include-ed <cmath>?

twomers 408 Posting Virtuoso

Yes.

Ever quit your job on a whim?

twomers 408 Posting Virtuoso
#include <ctime>
#include <iostream>

int main( void ) 
{
  int i;    
  
  srand(time(NULL));    
  i = rand()%200;  
  std::cout <<"Your random number is " <<i <<std::endl;  
  
  return(0);
}

Know how to loop?

twomers 408 Posting Virtuoso

Well most of us aren't mind readers here so provide some errors for us to look at.

>> #include "C:\Graphics\graphics.h"

replace \ with \\

Might/mightn't solve some of your problems.

twomers 408 Posting Virtuoso

I live in Ireland. It doesn't make much difference (But no).


Ever gone sailing?

twomers 408 Posting Virtuoso

Use code tags.

bool determine_square (int& length, int& width)
{
    if (length == width)
    
        square = true;

    else
        square = false;
    
    return square;

}

That should simply be:

bool determine(int &length, int &width) {
  return length==width;
}

In your function you make a reference to square but the scope of C++ defines that it is out of scope there, ie you can't access it. The variable square is only local within main itself.

Also you might want to consider not creating temp variables every time use a function. You can simply "return sqrt (length * length + width * width) / 12.0;", in the get diagonal function. Same with the area and perimeter functions.

twomers 408 Posting Virtuoso
twomers 408 Posting Virtuoso

Did you just add some getlines? It's hard to see what you did differently. Could you maybe highlight it in your code?

twomers 408 Posting Virtuoso

So overall thoughts? Good, bad much the same?

twomers 408 Posting Virtuoso

Yes. I think capricorn is below cancer ... I have one of my inverse memory things clinking in my brain saying so ...

You you sometimes read your email spam?

twomers 408 Posting Virtuoso

Well what do you need in the class? You need a variable to store the length, the width and the character to define the border. You'll want to be able to set all those parameters, so you can introduce some 'setters', you could maybe overload the ostream >> operator. You might want to see what values you're calling. You'll want a printing method and maybe an ostream << operator too.

twomers 408 Posting Virtuoso

>> Jail for speeding?
Speeding. Dangerous driving.
They like to make examples of extreme cases, I reckon.

twomers 408 Posting Virtuoso

Yes. FOr the third part don't endl every iteration. Just print a new line before the for loop.

twomers 408 Posting Virtuoso

I dunno. It's always good to resurect 2 year old threads!

twomers 408 Posting Virtuoso

Shouldn't use void main. getch() isn't a great way of stopping the program. iostream.h is non-standard. use iostream:

#include <iostream>

using namespace std;

int main( void ) {

  std::cin.ignore();
  return 0;
}
twomers 408 Posting Virtuoso

I think you misread, Ancient.

>> string words[6];

so there is a words[5].

twomers 408 Posting Virtuoso

Show an attempt. Don't double post. And don't PM for one to one help!

twomers 408 Posting Virtuoso

Not in the sense of power. But their convenience makes them better, in my opinion.

Is your room tidy?

twomers 408 Posting Virtuoso

No. (I'm mighty negative today).

twomers 408 Posting Virtuoso

Did you run the program before asking?

twomers 408 Posting Virtuoso

Nah. The problem was the program was terminating before he could see the output, so the 'pausing' code sorted it out.

twomers 408 Posting Virtuoso

I left some comments:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(void){ 

  char char1;
  int xR,yC; 

  cout << "Character" << endl;
  cin >> char1;

  cout << "Number of columns: ";
  cin >> yC;

  cout << "Number of rows: ";
  cin >> xR;


  for ( int y = 0; y < yC; y++ )
  {
      cout << char1; 
  }
  for ( int x = 0; x < xR; x++ )
  {
      cout << char1 /* BUFFER WITH SPACES AND PRINT char1 AGAIN */<< endl;
  }
  // PRINT THE FIRST ONE AGAIN


}

Also look at this:

#include <iostream>
#include <string>

int main(void){ 
  char bc = '#';
  
  std::string bt( 5, bc ), buf( 3, ' ' );

  std::cout<< bt << "\n";
  std::cout<< bc << buf << bc << "\n";
  std::cout<< bt << "\n";

  return 0;
}
twomers 408 Posting Virtuoso

No.

twomers 408 Posting Virtuoso

Sorry. I'm having fierce problems reading people's threads today!

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";

  cout << "How old are you? ";
  getline (cin, mystr);
  cout << "I am " << mystr << " too!\n";

  cin.ignore();
  cin.get();

  return 0;
}

You can put in both your details? Your age and name. It's just closing, right?

twomers 408 Posting Virtuoso

You really have all the info you need. I don't think we should help you till you show us at least a new attempt.

twomers 408 Posting Virtuoso
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "How old are you? ";
  cin.ignore()[B];[/B]
  getline (cin, mystr);
  cout << "I am" << mystr << " too!\n";
  return 0;
}
twomers 408 Posting Virtuoso

Don't think of a csv file as anything other than a file. You can access it and everything just like any other file (text file you mentioned).

What you want to do is not hard but it requires more than appending data. The simplest way of managing this (I reckon), is to create a vector of a vector of strings:

std::vector< std::vector<std::string> > csv_file_contents;

don't forget to #include <vector> and <string>. First thing is to read in the current file. I reckon line by line and tokenise with comas. The manipulation becomes much easier then. You can .erase() or .insert() or push back easily enough. Then save the contents of the csv file each time.

Note - this is not a good solution if you have a large csv file! And a better method would be to use two files - one is your first file, and the second is a progress-file. Say you want to change a line. Read each line in your first file and save to the second until you come to the line you want to change (which could be inserting or deleting lines/columns, cells etc). Then you can change that line and save to the second file. Read the rest of the first file until it's gone. Then close the streams. Delete the first file. Rename the second and you have it. You could also use fstream as opposed to if and ofstreams.

twomers 408 Posting Virtuoso

The problem is common in mixing cin and getlines. I won't go into the explanation (it's all over the place), but I'll tell you the solution. cin.ignore() before getline().

Also please use code tags when posting code. This is an easy code-sample to read because it's so small. But it becomes impossible to coherently analyse larger code without them.

twomers 408 Posting Virtuoso

Sometimes. But I prefer to reap someone else's benefits :)

Do you have a laptop?

twomers 408 Posting Virtuoso

About the spaces it might be worth making a string to do them rather than looping:

#include <iostream>
#include <string>

int main( void ) {
  std::string spaces( 10, ' ' );
  std::cout<< '#' << spaces << "#\n";

  return 0;
}

*checks OP's restrictions*

Ancient Dragon commented: good idea +19
twomers 408 Posting Virtuoso

Yeah ... So reading only the last post or two is not a great idea anymore.

twomers 408 Posting Virtuoso
for ( int j = 0; j < 5; j++ )
  {
    string tmp = crap[j];
    //output first and third character
    cout << tmp.substr ( 0,1 ) << " " << tmp.substr( 2,1 ) <<endl;
  }

Wouldn't it make more sense to to:

for ( ... ) {
  std::cout<< crap.at(j).at(1) << " " << crap.at(j).at(3);
//  std::cout<< crap[j][1] << " " << crap[j][3];
}

or use the [] operator, of course (though at throws ... not so certain about []) instead of constructing a string for every iteration of the loop? Obviously use substr if you want more than one character.

Tut tut tut. Giving away all the code...

twomers 408 Posting Virtuoso

ALso a space isn't going to be a good method of word termination specification. Some people like to put two spaces after their full stops. Some people don't put spaces after comas etc. What you could use (though it probably is a bit overkill), is use strtok - http://www.cplusplus.com/reference/clibrary/cstring/strtok.html It's easy to modify to suit your purposes.

twomers 408 Posting Virtuoso

Well technically then because for him it was incorrect it was a helpful prod in the right direction. All he had to do was 'flatten' it, right? So if two grey cells can rub off one another to create a fire ... you'd expect I could read propah.

twomers 408 Posting Virtuoso

Choking -> Rugby (really. I had an experience)

twomers 408 Posting Virtuoso

>> Leave them to their own devices
I know. I normally don't but I misread the first post and apparently messed up the second. Fat load of use I am, eh?

I should start reading the posts more closely. Dave's method is probably what they want. Why don't you give out to Dave? (Kiddin').

twomers 408 Posting Virtuoso

So you wrote the code but you don't know how to call a function ... really? That's pretty hard to believe.

Just call it like how you'd call any other function and pass the appropriate values into it!

twomers 408 Posting Virtuoso

Oh sorry. I misread. Please forgive :) For my sins -

#include <stdio.h>

int sum_digi( const int &num ) {
  if ( !num ) 
    return num;
  else
    return (num%10)+sum_digi( num/10 );
}

int main( void ) {
  int sum = sum_digi( 12345 );

  printf( "Sum: %d\n\n", sum );

  return 0;
}

Just simple recursion. What it does is it starts at 5 and works to 1. It gets the last digit (through the %10 operation), and then gets the next value through recursively calling the same function but dividing by ten to get the next digit (ie from 5 to 4 in this case).

It'll also work for bigger numbers.

twomers 408 Posting Virtuoso

Do You Always Capitalise The First Letter Of Each Word? :D
Welcome!

twomers 408 Posting Virtuoso

Pink (brings out my eyes).

Black tea/coffee or tea/coffee with milk?

twomers 408 Posting Virtuoso

I didn't think it looked like vomit, sulley ... not a fan of dates though ... the fruit I mean. Good though?

twomers 408 Posting Virtuoso

I saw it first and thought kmph. It's crazy, eh? Pass the alps, zan, go for the himalayas!