C++

Re: C++

Post by Acidiferous on Tue Nov 13, 2012 4:51 am
([msg=70811]see Re: C++[/msg])

I tried to run centip3de's code yesterday evening, and endet up doing a few changes. Not that is wassent fine.
But now that you got the code in your editor, it's hard not to start addding a little here and there.

Code: Select all
#include <iostream>
#include <sstream>

int main()
{
    int iCount = 0, iResult = 0;
    std::string sInput;
    while(iCount < 20)
    {
        int iNumIn;
        while (true)
        {   
            std::cout << "Enter a number: ";
           
            std::getline(std::cin, sInput);
           
            std::stringstream sStream(sInput);
            if (sStream >> iNumIn)
                 break;
            std::cout << "I dont think that was a number. \n";
        }
        bool isPositive = iNumIn > 0;

        if(isPositive)
        {
            iResult += iNumIn;
        }
        iCount++;
    }
    std::cout << "And the final calculation is... " << iResult << "!";
    std::cin.get();
    return 0;
}


I changed the endl to a \n. I remember learning that it is bad practice flushing if there is no reason for it. I havent used c/c++ for a long time, but i found this: http://stackoverflow.com/questions/5492 ... 05#5492605
Acidiferous
Experienced User
Experienced User
 
Posts: 62
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: C++

Post by Acidiferous on Tue Nov 13, 2012 4:51 am
([msg=70812]see Re: C++[/msg])

Sorry, double post.
Acidiferous
Experienced User
Experienced User
 
Posts: 62
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: C++

Post by shdwcat on Tue Nov 13, 2012 7:55 am
([msg=70814]see Re: C++[/msg])

Hey guys I am really thankful for the info but I am not trying to start arguments on here. So I have been looking and I have gotten the code below but its not just giving me the sum correctly. also if you input a neg number is in the beginning it doesnt sum. Any suggestions?

#include<iostream>
using namespace std;

int main()
{
cout << "Enter a series 20 of numbers with a space between numbers ";
int number;
int sum = 0;
for(;;)
{
cin >> number;

if( number <= 0)
{
cout << sum << endl;

}
sum += number;
}
system("PAUSE");
return 0;
}
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: C++

Post by Acidiferous on Tue Nov 13, 2012 8:13 am
([msg=70815]see Re: C++[/msg])

There you go. Made a few simple changes, you should be able to see the diffrence.

Code: Select all
#include<iostream>
using namespace std;

int main()
{
cout << "Enter a series 20 of numbers with a space between numbers ";
int number;
int sum = 0;
for(int iCount = 0;iCount < 20; iCount++)
{
     cin >> number;

     if(!(number <= 0))
     {
      sum += number;
      //cout << sum << endl;
     }
}
cout << "sum = " << sum; 
cin.ignore();
cin.get(); //system("PAUSE");
return 0;
}
Acidiferous
Experienced User
Experienced User
 
Posts: 62
Joined: Tue Mar 29, 2011 9:49 am
Location: Europe
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Tue Nov 13, 2012 3:10 pm
([msg=70821]see Re: C++[/msg])

shdwcat- It wasn't an argument, it's just how HTS criticizes code.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: C++

Post by shdwcat on Tue Nov 13, 2012 3:12 pm
([msg=70823]see Re: C++[/msg])

Acidiferous wrote:There you go. Made a few simple changes, you should be able to see the diffrence.

Code: Select all
#include<iostream>
using namespace std;

int main()
{
cout << "Enter a series 20 of numbers with a space between numbers ";
int number;
int sum = 0;
for(int iCount = 0;iCount < 20; iCount++)
{
     cin >> number;

     if(!(number <= 0))
     {
      sum += number;
      //cout << sum << endl;
     }
}
cout << "sum = " << sum; 
cin.ignore();
cin.get(); //system("PAUSE");
return 0;
}



Thx Acidiferous that is what I needed. Thx all for the help. :D
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: C++

Post by WallShadow on Tue Nov 13, 2012 4:50 pm
([msg=70828]see Re: C++[/msg])

not_essence2 wrote:shdwcat- It wasn't an argument, it's just how HTS criticizes code conquers the world.


yes... yes...
we shall use this 'argument' to spread our influence over the weak. and when the time comes, THE GEEKS SHALL INHERIT THE EARTH!!11!!11!!11
User avatar
WallShadow
Contributor
Contributor
 
Posts: 626
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++

Post by not_essence2 on Tue Nov 13, 2012 5:16 pm
([msg=70831]see Re: C++[/msg])

That depends. If the "weak" are criticism-resilient, you're going to have a bad time.
not_essence2
Poster
Poster
 
Posts: 189
Joined: Fri Sep 14, 2012 6:09 pm
Blog: View Blog (0)


Re: C++

Post by LoGiCaL__ on Tue Nov 13, 2012 7:44 pm
([msg=70841]see Re: C++[/msg])

I know it's hella late but I was bored plus it's been a while since I used c++ so here's my take on it:

Code: Select all

#include <iostream>

int main()
{
    int number, iterator, total = 0;

    for(iterator = 0; iterator < 20; ++iterator)
    {
        std::cout << "Enter a number: ";
        std::cin >> number;

        if(number > 0)
            total += number;

        number = 0;

    }

    std::cout << "Total = " << total << "\n";


    return 0;

}

User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1063
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C++

Post by WallShadow on Tue Nov 13, 2012 7:44 pm
([msg=70842]see Re: C++[/msg])

not_essence2 wrote:That depends. If the "weak" are criticism-resilient, you're going to have a bad time.


>.>

there's a reason they're called the 'weak'.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 626
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


PreviousNext

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests