473,654 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

not sure what im doing wrong with these if else statements

1 New Member
the first one works but the second does not when its called upon later

if (major == 1)
{
if (gpa > largestm1)
{
largestm1 = gpa;
highnamem1 = last;

}

else if (major == 2)
{

if (gpa > largestm2)
{
largestm2 = gpa;
highnamem2 = last;
}

}

and the next one wont work correctly at all

if (credits <= 30)
{
credits1 = students;
}
else if (credits >=31 && credits <=60)
{
credits2 = students;
}
else if (credits >=61 && credits <=90)
{
credits3 = students;
}
else if (credits >=90)
{
credits4 = students;
}
}

I appreciate any help you can give
Apr 26 '10 #1
2 1138
Fr33dan
57 New Member
When trying to determine errors with nested code blocks indenting each block in helps you see errors:
Expand|Select|Wrap|Line Numbers
  1. if (major == 1)
  2. { // Indent starting here because this is the start 
  3.   // of a code block.
  4.     if (gpa > largestm1)
  5.     {
  6.     largestm1 = gpa;
  7.     highnamem1 = last;
  8.  
  9.     }
  10.   // Now we know the next statement shouldn't go in the
  11.   // code block because it's not indented
  12. } // and we can see that we missed the end bracket.
  13. else if (major == 2)
  14. {
  15.  
  16.     if (gpa > largestm2)
  17.     {
  18.     largestm2 = gpa;
  19.     highnamem2 = last;
  20.     }
  21.  
  22. }
Expand|Select|Wrap|Line Numbers
  1. if (credits <= 30)
  2. {
  3.     credits1 = students;
  4. }
  5. else if (credits >=31 && credits <=60)
  6. {
  7.     credits2 = students;
  8. }
  9. else if (credits >=61 && credits <=90)
  10. {
  11.     credits3 = students;
  12. }
  13. else if (credits >=90)
  14. {
  15.     credits4 = students;
  16. }
  17. } // We should end go back a step at the end bracket 
  18.   // but we don't have anywhere to go so this must
  19.   // be an accidental extra (possibly added to stop 
  20.   // missing bracket errors).
Apr 27 '10 #2
jkmyoung
2,057 Recognized Expert Top Contributor
What does students refer to?
What do credits1, credits2, credits3, and credits4 refer to?
Apr 27 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2499
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a if statement by itself works just fine. Below is an example: if x >=0: DO SOMETHING Would it be better, or perhaps more complete, written like this:
11
1549
by: AJ | last post by:
here's the scenario.. I am running a DTS to collect the summarized info from Oracle database into SQL server. I then have a update job which updates my transactional table from the summarized table. The update takes a very long time (~ 3 minutes)even though it has around 1500 rows which causes the application to timeout. I want this job to be done in less than a minute.
16
2067
by: Shelly | last post by:
(posted previously on comp.lang.php but no response received. Cross-posted in the dreamweaver forum) I am confused about what goes on with method POST. Here is an overview of a my code, sketching it out: <?php if (isset($_POST) && $_POST=="Submit") { ---Do a bunch of stuff--- if (isset($_SESSION)) unset($_SESSION);
0
1291
by: John Phelan | last post by:
I had a very large number of query statements that I had converted to SQL a long time ago aticipating that I would some day upsize my application. Every now and then when I do an import to a new application, some of the forms will import corrupted, and crashing the whole application, though it works fine in the old application. Today I did some experimenting on the two most recent corrupt form files. I discovered that even when I...
0
1233
by: jpatrick | last post by:
I had a very large number of query statements that I had converted to SQL a long time ago aticipating that I would some day upsize my application. Every now and then when I do an import to a new application, some of the forms will import corrupted, and crashing the whole application, though it works fine in the old application. Today I did some experimenting on the two most recent corrupt form files. I discovered that even when I...
4
1289
by: Stefan Kowalski | last post by:
I recently posted a question which was answered by Allen Browne and gave me some tips to structure the tables. However, when it comes to searching the database, performance is unacceptably slow and I am now thinking of doing the unthinkable with my tables and wonder whether anyone has used (or still uses) this sort of approach. The database stores libraries and classifies the types of book they keep. So I have the classic 3-table...
9
1461
by: David Teran | last post by:
Hi, we are currently using another database product but besides some licensing issues we are finding more and more problems with the database. We are evaluating PostgreSQL and it looks quite handy. Interesting features paired with good performance and a lot of users seems to be a prove for the quality. I wonder if people encountered bugs like 'a select returns a wrong number of rows' or any other very serious bugs. We do not have...
42
3428
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
4
2020
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
16
1904
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a simpler answer, by all means show me the light! What I'm trying to do is refresh the page at a timed interval ( actually redirect the page... ) and I have a simple piece of code I got from the net that works, but I need to modify it a little...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.