C/C++ Beginner's Guide and Post Basic Questions here

theterminator

Wise Old Owl
Joined
Mar 28, 2013
Messages
1,951
int main() {
ifstream f("Words.cpp");
int nwords = 0;
string word;

while (f >> word)
++nwords;

cout << "Number of words = " << nwords << endl;
}
what does the thing in bold mean?
 

harshilsharma63

DIY FTW!
Joined
Jul 2, 2012
Messages
6,430
what does the thing in bold mean?

Yes you can do it using string.

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
fstream file;
string line;

file.open("E:\\abc.txt", ios::in);

while(getline(file, line))
cout << line << endl;

return 0;
}
 
Last edited:

theterminator

Wise Old Owl
Joined
Mar 28, 2013
Messages
1,951
Yes you can do it using string.

PHP:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
fstream file;
string line;

file.open("E:\\abc.txt", [B]ios::in[/B]);

while(getline(file, line))
cout << line << endl;

return 0;
}

why php? what is ios::in??
 

harshilsharma63

DIY FTW!
Joined
Jul 2, 2012
Messages
6,430
why php? what is ios::in??

Its C++ code enclosed in
PHP:
 tag. Edited it :wink:

ios::in is the file opening mode. When you open a file, you have to specify the opening mode. It can be read only, white only, append with in, out, app being their specifiers. These file openning specifiers reside in ios class (not sure if its a class), hence, the use of ios::in. If the file were to be apened for writing only, then it would be ios::out.
 

kunalht

In the zone
Joined
Sep 7, 2012
Messages
392
please suggest a C/C++ software for ubuntu (linux).
I am new to ubuntu.
So please suggest a good software with compiler with download links.
also tell me some good softwares for linux.
 

Chaitanya

Cyborg Agent
Joined
Jul 7, 2011
Messages
1,393
please suggest a C/C++ software for ubuntu (linux).
I am new to ubuntu.
So please suggest a good software with compiler with download links.
also tell me some good softwares for linux.

I have no programming experience on linux tho heard it has inbuilt libs for C compilers.
You may try GCC(GNU Compiler Collection)GCC, the GNU Compiler Collection - GNU Project - Free Software Foundation (FSF)

You may try notepad++ for editing & creating codes..
 

nims11

BIOS Terminator
Joined
Apr 29, 2008
Messages
980
please suggest a C/C++ software for ubuntu (linux).
I am new to ubuntu.
So please suggest a good software with compiler with download links.
also tell me some good softwares for linux.

gcc, g++, Codeblocks.
 

ico

Super Moderator
Staff member
Joined
Jun 14, 2007
Messages
11,181
Clang is the compiler of my choice these days. Error reporting is much better.
 

aaruni

The Linux Guy
Joined
Mar 17, 2012
Messages
1,170
Can anyone tell me why this program only prints "<year> is not a leap year." ?

Code:
/*
Fourth problem
input -> num year
output -> if leap year -> true , else -> false (write proper interface)
conditions of leap year : divisible by 4 !by 100 but by 400
*/
#include<iostream>
using namespace std;
int main()
{
	int year,limb1,limb10,limb40;
	cout<<"Program to tell whether a given year is a leap year or not.\nPlease enter the year to be checked.\n\t";
	cin>>year;
	limb1=year%4;
	if (limb1 == 0)
	{
		limb10 = year%100;
		limb40 = year%400;
		if (limb40 == 0)
		{
			if (limb10 > 0)
			{
				cout<<year<<" is a leap year.\n";
			}
			else if (limb10 == 0)
			{
				cout<<year<<" is not a leap year.\n";
			}
		}
		else
		{
			cout<<year<<" is not a leap year.\n";
		}
	}
	else
	{
		cout<<year<<" is not a leap year.\n";
	}
	return 0;
}
 

Chaitanya

Cyborg Agent
Joined
Jul 7, 2011
Messages
1,393
Can anyone tell me why this program only prints "<year> is not a leap year." ?

Code:
	cin>>year;
	limb1=year%4;
	if (limb1 == 0)
	{
		[B]limb10 = year%100;
		limb40 = year%400;[/B]
		if (limb40 == 0)

Lost there...
Why that segment??
 

Chaitanya

Cyborg Agent
Joined
Jul 7, 2011
Messages
1,393
It was a logical error on my part. Corrected here : C/C++ Thread

Well you could have written program a lot smaller..
Couldn't get why you required 3 conditions while it is solved by one.

Well problem is solved & only that matters. ;-)
 

aaruni

The Linux Guy
Joined
Mar 17, 2012
Messages
1,170
condition of a leap year are specified in the program comments.

conditions of leap year : divisible by 4 !by 100 but by 400
 

Chaitanya

Cyborg Agent
Joined
Jul 7, 2011
Messages
1,393
condition of a leap year are specified in the program comments.

conditions of leap year : divisible by 4 !by 100 but by 400

Hmm.. I see, I totally overlooked the Centurial years condition.... LOL on myself.. :-D
 

rohitshubham

Ambassador of Buzz
Joined
Jul 20, 2008
Messages
550
so, i was doing a code and submitting it online i encountered a problem i.e. when i submit code #1 it accepts the solution but when i try code #2 it gives wrong answer , but as far as i know, the logic is same. plz help.
code no 1:
Code:
int main(){
  int T;
  long int R;
  long int x1,y1;
  long int x2,y2;
  long int x3,y3;
  long int d1,d2,d3;
 
  scanf("%d",&T);
  while(T > 0) {
    scanf("%d",&R);
    scanf("%d %d",&x1,&y1);
    scanf("%d %d",&x2,&y2);
    scanf("%d %d",&x3,&y3);
    R = pow(R,2);
    d1 = pow((x1-x2),2) + pow((y1-y2),2);
    d2 = pow((x1-x3),2) + pow((y1-y3),2);
    d3 = pow((x2-x3),2) + pow((y2-y3),2);
    if((d1 <= R && d2 <= R) || (d2 <= R && d3 <= R) || (d1 <= R && d3 <= R)) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
 
    T--;
  }  /* while */
  return 0;
}
code no 2:
Code:
int main(){   int test;
    float x1,x2,x3,y1,y2,y3,r;
    float dis1,dis2,dis3;
    cout<<"enter test cases";
    cin>>test;
    while(test--)
    {
        cin>>r>>x1>>y1>>x2>>y2>>x3>>y3;
        dis1= pow((x1-x2),2) + pow((y1-y2),2);
        dis2=pow((x2-x3),2)+ pow((y2-y3),2);
        dis3=pow((x1-x3),2)+pow((y1-y3),2);
        r=pow(r,2);
        if((dis1<=r && dis2<=r)||(dis1<=r && dis3<=r)||(dis3<=r && dis2<=r))
            cout<<"yes";
        else
            cout<<"no";


    }
    return 0;
}
 
Last edited:

Chaitanya

Cyborg Agent
Joined
Jul 7, 2011
Messages
1,393
^^
1. in code #1 variables are long type & in #2 they are float type
2. #1 is in C & #2 is in cpp
 
Top