Nikhar 19 Junior Poster in Training

Hey guys...Can you please help me explain what does returning an istream object mean.

I am currently studying Accelerated C++ and at many places a function returns istream object. What does it mean?

For example:-

istream& read(istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework); // read and store all the student's homework grades
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear() ;
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}

What do the two functions mean? Basically, I want to ask what does passing an istream object mean and returning an istream object do?

And they are using "is" with ">>". Isn't cin used there?

Nikhar 19 Junior Poster in Training

Hi...when I started Code::Blocks, I got a window that said that it detected GNU GCC compiler. Logic says that I already have it installed what I'm looking for.

So, is that correct?

PS:- Sorry for showering you with such stupid questions.

Nikhar 19 Junior Poster in Training

@ niek_e

Can you please tell me how do I do that?

Nikhar 19 Junior Poster in Training

Is codeblocks an ANSI compiler compatible with gcc/g++/gpp (the GNU C/C++ Compilers)?

Nikhar 19 Junior Poster in Training

Hi....it seems my firefox and my opera has gone barmy. Opera would automatically open new tage and in theadress bar, I would see this--> opera:illegal-url-2.

Firefox would open a new window with loads of tags in it and c:/directory in each of them.

Can anyone help me?

Nikhar 19 Junior Poster in Training

Ok...Thanks for your help! :)

This should work.

Nikhar 19 Junior Poster in Training

This is quite simple actually,

Put all of the Numbers into a array,

Then Generate a random number between 0 and {The Number of Numbers in your array } Including 0 too, So then you can use array [randomnumberindex] to get a random number out,

Oh...lol...okay...yeah..that was simple. :D

Nikhar 19 Junior Poster in Training

Thanks Grigor. Would check the books soon.

Thanks daviddoria for your advice.

Thanks tux4life. Would check the books soon.

Thanks Tom Gunn for your advice. Would check the book.

Thanks Siddhanth for your advice.

More advices would be appreciated.

Nikhar 19 Junior Poster in Training

Thanks...I'll be checking that out.

Nikhar 19 Junior Poster in Training

These two code snippets may be useful. No vectors, not goto statements.

http://www.daniweb.com/code/snippet1034.html
http://www.daniweb.com/code/snippet1179.html


You're in a tough bind. It's unfortunate that, for some strange reason, many Computer Science teachers insist on using outdated compilers when there are modern, free compilers out there. Probably it's what they are used to and they simply aren't willing to take the time to update their knowledge. They are doing you a real disservice, in my opinion, because everyone else you interact with is going to be using modern compilers. Your code won't work on their compiler and their code won't work on your compiler, so you have a choice to make. You can either do things the "right" (i.e. modern) way and suffer the wrath of your professor, or you can use the outdated compiler, which will please your professor, but will please no one else. It's unfair to the students and they really need to shame these professors into teaching with modern compilers.

Hi...thanks for your concern. :)

Well, what is "bool" data type? (saw it in the code snippets you specified)

And, well, I wonder what can our teachers do, if the course specified by the board is old.

Anyways, the option that I will have to use is, use old c++ in scool and modern c++ here..lol....that's the only feasible reason there seems.

Nikhar 19 Junior Poster in Training

Thanks for help everyone. Well, the problem si that I don't know how a printf function works. A link to guide on using printf would be highly appreciated.

Nikhar 19 Junior Poster in Training

Well, I posted a few problems here on daniweb and thank god that I did so. What I had been using till now was "rusted, naff,useless" code. But after today, I am more accustomed to what the new c++ is.

Well, I am 16 years old and am studying in standard 10+2.

The problem is the way we are taught in school. In school, we still use
#include<iostream.h>,#include<conio.h>,getch(),clrscsr()...etc.etc....


Considering the fact that I have already had 1.5 years of study in c++(using the old ways), you may guess my current condition.
I referred to Siddhants guide and found it quite useful. Thanks. :)

Well, coming to the point on what I want...

If anyone could help me and provide me with link to the better tutorials on Standard and modern C++, I would be highly grateful.

Well, I don't know how to use vectors. So,a guide on vectors would be useful.

Also, a list of modern header files would be extremely useful. From my information, conio.h is not in use anymore. So, how can I use stuff like cout, cin, etc.

What are namespaces?

And a list of things and functions which are outdated now, and the new ones to replace them would be great.

Thanks a ton in advance. Any help would be greatly appreciated.

Nikhar 19 Junior Poster in Training

Please try my below code
it will generate the randoam number which you want.
the problem you shd reset the count varaible and j shd start from 0 to <i

#include<conio.h>
#include<stdlib.h>
#include<time.h>

  main()
{
	int i,j,random_integer,count=0,sno[20];
	srand((unsigned)time(0));
	sno[0]=(rand()%20)+1;
	for(i=0;i<=19;i++)
	{
lbl1:	random_integer=(rand()%20)+1;
		for(j=0;j<i;j++)
		{
			if(sno[j]==random_integer)
				break;
			count++;
		}
		if(count==i)
		{
                    count =0;                    
			sno[i]=random_integer;
        }
		else
		{
            count =0;
			goto lbl1;
        }
	}
	for(i=0;i<=19;i++)
		printf("%d\t",sno[i]);
		getch();

}

And your code worked too. Thanks a ton!
With my previous code, it was going in an infinite loop. Thanks once again.

Nikhar 19 Junior Poster in Training

Oh, what a junk piece of codes (Post #1 and Post#3). It is pretty much expected from Turbo C++ users. Your code is rusted. It is the C++ what it used to be 13 years ago. Now the world has changed. Most of the C++ developer use the Standard C++. They will find hard to read/debug your code. I prefer that you should switch to standards right now.
Read this guide to migrate to standards. It suits best to you both.

Goto is another sinful statement to use in your code. Never use goto.

Just to demonstrate the power of using standard C++, look at the program below. It has been written in standard c++ and does the work which you want to accomplish, but in less amount of line and in less amount of time:

#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
    std::vector<int> vec(20);//create a vector of 20 ints
    for(int i=1;i<=20;++i) vec[i]=i;
    random_shuffle (vec.begin(), vec.end());//suffle them randomly
    
    for(size_t i=0;i!=vec.size();++i) 
        std::cout<<vec[i]<<std::endl;
}

Output:

4
10
11
15
14
16
17
1
6
9
3
7
19
2
0
12
5
18
13
8

But the above code won't obviously run in your crappy old good-for-nothing compiler called Turbo C++.
Please start using a standard compiler. Read the guide for more information.

Edit: I guess I was too slow!! Anyways, the word is deprecated and not depreciated.
Edit2: I forgot to mention a good shuffling algorithm: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle . Remember it is an algorithm and not a working …

Nikhar 19 Junior Poster in Training

Hi all.

I'm trying to make a program where there is an array of 20 elements. Now, numbers from 1 to 20 will be assigned in a random order in the array.

This is my code:-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
	int i,j,random_integer,count=0,sno[20];
	srand((unsigned)time(0));
	sno[0]=(rand()%20)+1;
	for(i=0;i<=19;i++)
	{
lbl1:	random_integer=(rand()%20)+1;
		for(j=1;j<=i;j++)
		{
			if(sno[j]==random_integer)
				break;
			count++;
		}
		if(count==i)
			sno[i]=random_integer;
		else
			goto lbl1;
	}
	for(i=0;i<=19;i++)
		cout<<"\n"<<sno[i];
}

When I execute this code in Turbo c++, Turbo c++ hang.
What is wrong with this code?

Nikhar 19 Junior Poster in Training

I'm using Turbo C++ 4.5

From where can I download codeblocks compiler?

Nikhar 19 Junior Poster in Training

Use std::fixed.

cout << std::fixed << a;

Hi...Thanks for a response.

But, I am getting the following errors:-

Type qualifier 'std' must be a struct or class anme in function main()

Statement misisng in function main()

The errors occur when the program is as follows:-

#include<iostream.h>
#include<conio.h>

void main()

{
	 long double a=100000000.0;
 cout<<std::fixed<<a;
 getch();
}
Nikhar 19 Junior Poster in Training

Do you mean how can you pick up a random number from given sequence of numbers?
follow this thread :http://www.daniweb.com/forums/thread198944.html

Lemme try and explain it in more details.
I provide the program with the following group of numbers:-

1,5,18,100,127,500,626,929.

Now, all I want is that the output should be a random number from one of these numbers:-

1,5,18,100,127,500,626,929.

Nikhar 19 Junior Poster in Training

I'll try to get this as simple as possible. The problem is fairly simple.

long double a=100000000;
cout<<a;

Well, the output this shows is "1e+08"

I do not want the output to be in exponential form. Rather, I want it to be printed as "100000000".

What shall I do?

Thnaks in advance.

Nikhar 19 Junior Poster in Training

Ok...thanks.

I figured out the way of getting around this.

suitcase::suitcase()
{

		amount[0]=0.25;
		amount[1]=1;
		amount[2]=10;
		amount[3]=100;
		amount[4]=1000;
		amount[5]=5000;
		amount[6]=10000;
		amount[7]=25000;
		amount[8]=50000;
		amount[9]=75000;
		amount[10]=100000;
		amount[11]=200000;
		amount[12]=300000;
		amount[13]=400000;
		amount[14]=500000;
		amount[15]=1000000;
		amount[16]=2500000;
		amount[17]=5000000;
		amount[18]=7500000;
		amount[19]=10000000;
}

But this is too long a way. Isn't there any other way?

Nikhar 19 Junior Poster in Training
#include<iostream.h>
#include<conio.h>

class suitcase
{
	int sno[20];
	long double amount[20];
	public:
	suitcase();

	void print()
	{
		int i;
		for(i=0;i<=19;i++)
		cout<<amount[i]<<'\n';
	}
};

suitcase::suitcase()
{

amount[20]={0.25,1.0,10.0,100.0,250.0,500.0,1000.0,5000.0,10000.0,50000.0,
100000.0,200000.0,300000.0,400000.0,500000.0,1000000.0,
2500000.0,5000000.0,7500000.0,10000000.0};


}

void main()
{
 suitcase s1;
 s1.print();

}

When I run this program, I get an error :-

expression syntax in function suitcase::suitcase()

Why am I getting an error? What am I doing wrong?

I know its a very stupid error, but can't spot which it is.

Kindly help me.

Thanks in advance.

Nikhar 19 Junior Poster in Training

Hi everyone.

Can anyone please guide me on how can I generate a random number from a group of prset numbers.


For example:-

There is a group of preset numbers:-
1,5,18,100,127,500,626,929.

I want to generate a random number from this group....is there anyway in which we could do this?

Nikhar 19 Junior Poster in Training

Hi...ok...I want to create a menu which would have 5-6 options.
Now, what I want is if the user is at the first option, if he uses the down arrow key, the cursor should move at the next option.
I don't want you to write the program for me or anything.

I just want you to guide me as to how shall I proceed in this program.

Thanks in advance.

Nikhar 19 Junior Poster in Training

I know the way of exploring it. It would work if I type "c:" in the address bar. It's just annoying to end up on the search page when we click on one of the local drives.

Thanks for your time though. :)

Nikhar 19 Junior Poster in Training

Ok...I'll try and explain this in as clearly as possible.
When I click on my computers, a new window opens up. Now when I click on any of the local drives:- C,D,E or F......instead of the drive a search page is opened.
I think I know the reason.
In file types in folder options, in drive extension, I have the search page as default. The problem is that I can't undefault(if there's a word of that sort) it nor can I remove search.

What shall I do?

Nikhar 19 Junior Poster in Training

http://www.dougknox.com/xp/file_assoc.htm

Download the fix for .exes

Thanks...thanks...thanks a thousand tons dude. It worked!

You are a life saver.:)

Nikhar 19 Junior Poster in Training

Hi....whenever I try top open a .exe file....this is the error that I get:-

The file does not have a program associated with it for performing this action. Create an association in the folder options in control menu.

I tried to add a new extension as exe in file types in folder options. But that did not work. Whenever I opened the folder options again, the exe file wouldn't be there.

What shall I do?
Any help would be much appreciated.

Nikhar 19 Junior Poster in Training

Hey understood the concept. Thanks a lot for your help.

Nikhar 19 Junior Poster in Training

Hey....thanks for your help. The code worked but its a bit confusing. Can you give another example?
Suppose, we want the following as output:-

This is an example to print \n and " "

Nikhar 19 Junior Poster in Training

Hi everyone,
This might be a very stupid question to ask and the answer might be very simple but I was just curious to know this.

How to display "\n" on output screen in C++?
We all know \n is used to enter a new line. But what if I want to print \n on screen.
Similarly, how to print ""(double quotes) on the screen?

Nikhar 19 Junior Poster in Training

*Correction

if(strcmp(str1,str2)==0)//with the header file <string.h> of course.

This shall work. :)

Nikhar 19 Junior Poster in Training

Hi...thanks everyone for your reply. I ultimately found the solution.

Yes, Agni, we can use string file in the header.

Instead of this:-

1.
if(str1==str2)

It should have been

if(strcmp(str1,str2))//with the header file <string.h> of course.

Nikhar 19 Junior Poster in Training

I don't understand what you want but check this . Its work

#include<iostream>
//#include<conio>

using namespace std;
char SearchString(char[2][10],char[10]);

int main()
{
//std::clrscr();
char str1[2][10],str2[10];
int i,j;
cout<<"\nEnter 5 objects:-";
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {
cout<<"\nEnter the string";
cin>>str1[i][j];
    }
}
for(i=0; i<10;++i)
{
cout<<"\nEnter char to be searched:-";
cin>>str2[i];
}

cout<<"\n\n\n"<<SearchString(str1,str2);
//getch();
}

char SearchString(char str1[2][10],char str2[10])
{
int i,j;
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {

if(str1[i][j]==str2[j])
return('F');

    }
}
return ('N');
}

Hi...perhaps, I should have made the needs of the program clearer.

User has to enter 5 names.
Suppose, he enters:-
Bat
Ball
Bag
Gloves
Pads

Then, the program asks the user for the string to be searched.
Suppose, he enters Bat.

The program should then print "F" on the screen because it found the string "Bat".

if(str1[i]==str2)

This line is error prone.

Hi, then what can be the correct line to replace this one?

Nikhar 19 Junior Poster in Training

Hi...this is the code that I wrote to find a particular string from a 2-d array of characters. It prints F(for found) and N(for Not found).
When I execute this program, it prints N only.

#include<iostream.h>
#include<conio.h>


char SearchString(char[5][10],char[10]);

void main()
{
	clrscr();
	char str1[5][10],str2[10];
	int i;
	cout<<"\nEnter 5 objects:-";
	for(i=0;i<=4;i++)
		cin>>str1[i];
	cout<<"\nEnter the string to be searched:-";
	cin>>str2;
	cout<<"\n\n\n"<<SearchString(str1,str2);
	getch();
}

char SearchString(char str1[5][10],char str2[10])
{
	int i;
	for(i=0;i<=4;i++)
	{
		if(str1[i]==str2)
			return('F');
	}
	return('N');
}